Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: github/gollum
base: 00b6f5c3fb
...
head fork: github/gollum
compare: dc91e0f733
Checking mergeability… Don't worry, you can still create the pull request.
  • 5 commits
  • 3 files changed
  • 0 commit comments
  • 2 contributors
Commits on Apr 12, 2012
@vmg vmg Allow indented code blocks in Gollum syntax 7317a9b
@vmg vmg Remove the GFM parser; it makes no sense
The new versions of GitHub::Markup will automatically render GFM for
Markdown documents if the necessary gems are installed.
0cc4c9f
@vmg vmg Keep a MarkupGFM class for compatibility reasons b5a2a8d
@vmg vmg Update README adc3abe
@atmos atmos Merge remote-tracking branch 'origin/sane-parsing' dc91e0f
Showing with 33 additions and 80 deletions.
  1. +10 −8 README.md
  2. +23 −67 lib/gollum/markup.rb
  3. +0 −5 test/test_markup.rb
View
18 README.md
@@ -34,7 +34,8 @@ to install the dependencies for the formats that you plan to use.
* [ASCIIDoc](http://www.methods.co.nz/asciidoc/) -- `brew install asciidoc`
* [Creole](http://wikicreole.org/) -- `gem install creole`
-* [Markdown](http://daringfireball.net/projects/markdown/) -- `gem install rdiscount`
+* [Markdown](http://daringfireball.net/projects/markdown/) -- `gem install redcarpet`
+* [GitHub Flavored Markdown](http://github.github.com/github-flavored-markdown/) -- `gem install github-markdown`
* [Org](http://orgmode.org/) -- `gem install org-ruby`
* [Pod](http://search.cpan.org/dist/perl/pod/perlpod.pod) -- `Pod::Simple::HTML` comes with Perl >= 5.10. Lower versions should install Pod::Simple from CPAN.
* [RDoc](http://rdoc.sourceforge.net/)
@@ -294,18 +295,19 @@ separately) by using the following syntax:
end
```
-The block must start with three backticks (as the first characters on the
-line). After that comes the name of the language that is contained by the
+The block must start with three backticks, at the beginning of a line or
+indented with any number of spaces or tabs.
+After that comes the name of the language that is contained by the
block. The language must be one of the `short name` lexer strings supported by
Pygments. See the [list of lexers](http://pygments.org/docs/lexers/) for valid
options.
-If the block contents are indented two spaces or one tab, then that whitespace
-will be ignored (this makes the blocks easier to read in plaintext).
-
-The block must end with three backticks as the first characters on a
-line.
+The block contents should be indented at the same level than the opening backticks.
+If the block contents are indented with an additional two spaces or one tab,
+then that whitespace will be ignored (this makes the blocks easier to read in plaintext).
+The block must end with three backticks indented at the same level than the opening
+backticks.
## MATHEMATICAL EQUATIONS
View
90 lib/gollum/markup.rb
@@ -361,17 +361,29 @@ def find_page_from_name(cname)
#
# Returns the placeholder'd String data.
def extract_code(data)
- data.gsub!(/^``` ?([^\r\n]+)?\r?\n(.+?)\r?\n```\r?$/m) do
- id = Digest::SHA1.hexdigest("#{$1}.#{$2}")
+ data.gsub!(/^([ \t]*)``` ?([^\r\n]+)?\r?\n(.+?)\r?\n\1```\r?$/m) do
+ id = Digest::SHA1.hexdigest("#{$2}.#{$3}")
cached = check_cache(:code, id)
@codemap[id] = cached ?
{ :output => cached } :
- { :lang => $1, :code => $2 }
- id
+ { :lang => $2, :code => $3, :indent => $1 }
+ "#{$1}#{id}" # print the SHA1 ID with the proper indentation
end
data
end
+ # Remove the leading space from a code block. Leading space
+ # is only removed if every single line in the block has leading
+ # whitespace.
+ #
+ # code - The code block to remove spaces from
+ # regex - A regex to match whitespace
+ def remove_leading_space(code, regex)
+ if code.lines.all? { |line| line =~ /\A\r?\n\Z/ || line =~ regex }
+ code.gsub!(regex, '')
+ end
+ end
+
# Process all code from the codemap and replace the placeholders with the
# final HTML.
#
@@ -387,16 +399,18 @@ def process_code(data, encoding = nil)
next if spec[:output] # cached
code = spec[:code]
- if code.lines.all? { |line| line =~ /\A\r?\n\Z/ || line =~ /^( |\t)/ }
- code.gsub!(/^( |\t)/m, '')
- end
+
+ remove_leading_space(code, /^#{spec[:indent]}/m)
+ remove_leading_space(code, /^( |\t)/m)
blocks << [spec[:lang], code]
end
highlighted = begin
encoding ||= 'utf-8'
- blocks.map { |lang, code| Pygments.highlight(code, :lexer => lang, :options => {:encoding => encoding.to_s}) }
+ blocks.map { |lang, code|
+ Pygments.highlight(code, :lexer => lang, :options => {:encoding => encoding.to_s})
+ }
rescue ::RubyPython::PythonError
[]
end
@@ -471,63 +485,5 @@ def update_cache(type, id, data)
end
end
- begin
- require 'redcarpet'
-
- class MarkupGFM < Markup
- def render(no_follow = false, encoding = nil)
- sanitize = no_follow ?
- @wiki.history_sanitizer :
- @wiki.sanitizer
-
- data = extract_tex(@data.dup)
- data = extract_code(data)
- data = extract_tags(data)
-
- if Gem::Version.new(Redcarpet::VERSION) > Gem::Version.new("1.17.2")
- html_renderer = Redcarpet::Render::HTML.new({
- :autolink => true,
- :fenced_code_blocks => true,
- :tables => true,
- :strikethrough => true,
- :lax_htmlblock => true,
- :no_intraemphasis => true
- })
- markdown = Redcarpet::Markdown.new(html_renderer)
- data = markdown.render(data)
- else
- flags = [
- :autolink,
- :fenced_code,
- :tables,
- :strikethrough,
- :lax_htmlblock,
- :no_intraemphasis
- ]
- data = Redcarpet.new(data, *flags).to_html
- end
- data = process_tags(data)
- data = process_code(data, encoding)
-
- doc = Nokogiri::HTML::DocumentFragment.parse(data)
-
- doc.search('pre').each do |node|
- next unless lang = node['lang']
- next unless lexer = Pygments::Lexer[lang]
- text = node.inner_text
- html = lexer.highlight(text)
- node.replace(html)
- end
-
- doc = sanitize.clean_node!(doc) if sanitize
- yield doc if block_given?
-
- data = doc.to_html
- data = process_tex(data)
- data
- end
- end
- rescue LoadError
- MarkupGFM = Markup
- end
+ MarkupGFM = Markup
end
View
5 test/test_markup.rb
@@ -456,11 +456,6 @@
# rendered with Gollum::Markup
page, rendered = render_page(content)
assert_markup_highlights_code Gollum::Markup, rendered
-
- if Gollum.const_defined?(:MarkupGFM)
- rendered_gfm = Gollum::MarkupGFM.new(page).render
- assert_markup_highlights_code Gollum::MarkupGFM, rendered_gfm
- end
end
def assert_markup_highlights_code(markup_class, rendered)

No commit comments for this range

Something went wrong with that request. Please try again.