Use String#sub instead of String#gsub when possible

pull/29/head
Thibaut 11 years ago
parent 8d5cb69aca
commit 864188e24c

@ -43,7 +43,7 @@ module Docs
end end
def slug def slug
@slug ||= subpath.gsub(/\A\//, '').gsub('.html', '') @slug ||= subpath.sub(/\A\//, '').sub(/\.html\z/, '')
end end
def root_page? def root_page?

@ -17,11 +17,11 @@ module Docs
end end
def format_url(url) def format_url(url)
url.to_s.gsub %r{https?://}, '' url.to_s.sub %r{\Ahttps?://}, ''
end end
def format_path(path) def format_path(path)
path.to_s.gsub File.join(File.expand_path('.'), ''), '' path.to_s.sub File.join(File.expand_path('.'), ''), ''
end end
def justify(str) def justify(str)

@ -8,7 +8,7 @@ module Docs
css('[id]').each do |node| css('[id]').each do |node|
# Module # Module
if node.name == 'h2' if node.name == 'h2'
type = node.content.gsub 'Backbone.', '' type = node.content.sub 'Backbone.', ''
if type.capitalize! # sync, history if type.capitalize! # sync, history
entries << [node.content, node['id'], type] entries << [node.content, node['id'], type]
end end

@ -6,8 +6,6 @@ module Docs
xpath('./text()', './/text()[not(ancestor::pre) and not(ancestor::code)]').each do |node| xpath('./text()', './/text()[not(ancestor::pre) and not(ancestor::code)]').each do |node|
content = node.content content = node.content
next unless content.valid_encoding? next unless content.valid_encoding?
content.gsub! %r{\A[[:space:]]+}, ' '
content.gsub! %r{[[:space:]]+\z}, ' '
content.gsub! %r{[[:space:]]+}, ' ' content.gsub! %r{[[:space:]]+}, ' '
node.content = content node.content = content
end end

@ -2,7 +2,7 @@ module Docs
class Http class Http
class EntriesFilter < Docs::EntriesFilter class EntriesFilter < Docs::EntriesFilter
def get_type def get_type
at_css('h1').content.gsub(/\A\s*HTTP\s+(.+)\s+Definitions\s*\z/, '\1').pluralize at_css('h1').content.sub(/\A\s*HTTP\s+(.+)\s+Definitions\s*\z/, '\1').pluralize
end end
def include_default_entry? def include_default_entry?

@ -33,7 +33,7 @@ module Docs
# Remove "- " before method names # Remove "- " before method names
css('.signature', 'span.overload').each do |node| css('.signature', 'span.overload').each do |node|
node.child.content = node.child.content.gsub(/\A\s*-\s*/, '') node.child.content = node.child.content.sub(/\A\s*-\s*/, '')
end end
# Remove links to type classes (e.g. Number) # Remove links to type classes (e.g. Number)

@ -3,7 +3,7 @@ module Docs
self.namespace = 'html_pipeline' self.namespace = 'html_pipeline'
def call_filter(event) def call_filter(event)
log "Filter: #{event.payload[:filter].gsub('Docs::', '').gsub('Filter', '')} [#{event.duration.round}ms]" log "Filter: #{event.payload[:filter].sub('Docs::', '').sub('Filter', '')} [#{event.duration.round}ms]"
end end
end end
end end

@ -17,7 +17,7 @@ class DocsCLI < Thor
Docs.all. Docs.all.
map { |doc| [doc.to_s.demodulize.underscore, doc] }. map { |doc| [doc.to_s.demodulize.underscore, doc] }.
each { |pair| max_length = pair.first.length if pair.first.length > max_length }. each { |pair| max_length = pair.first.length if pair.first.length > max_length }.
each { |pair| puts "#{pair.first.rjust max_length + 1}: #{pair.second.base_url.gsub %r{https?://}, ''}" } each { |pair| puts "#{pair.first.rjust max_length + 1}: #{pair.second.base_url.sub %r{\Ahttps?://}, ''}" }
end end
desc 'page <doc> [path] [--verbose] [--debug]', 'Generate a page (no indexing)' desc 'page <doc> [path] [--verbose] [--debug]', 'Generate a page (no indexing)'

@ -124,7 +124,7 @@ class DocsFileStoreTest < MiniTest::Spec
describe "#each" do describe "#each" do
let :paths do let :paths do
paths = [] paths = []
store.each { |path| paths << path.gsub(tmp_path, '') } store.each { |path| paths << path.sub(tmp_path, '') }
paths paths
end end

Loading…
Cancel
Save