Permalink
Browse files

Implement github format markdown code fences.

RDoc::Markup::Verbatim#== now considers formats
  • Loading branch information...
drbrain committed Mar 17, 2012
1 parent f8016a2 commit ed5f3d34cbcd0608429da33077687c71727f92e1
View
@@ -42,6 +42,7 @@ API changes to RDoc
* Rename TopLevel to File
* Rename Context to Container
* Rename NormalClass to Class
+* Add versions to RDoc::Markup syntax tree marshal format
=== Crazy Ideas
View
@@ -23,6 +23,15 @@
# h1 { font-size: 3em }
# </style>
#
+# ### Github
+#
+# Enables a partial set of [github flavored markdown] [GFM]. This extension is
+# enabled by default.
+#
+# #### Fenced code blocks
+#
+# Use ` ``` ` around a block of code instead of indenting it four spaces.
+#
# ### HTML
#
# Enables raw HTML to be included in the output. This extension is enabled by
@@ -81,6 +90,7 @@
#
# [syntax]: http://daringfireball.net/projects/markdown/syntax
# [pegmarkdown]: https://github.com/jgm/peg-markdown
+# [GFM]: http://github.github.com/github-flavored-markdown/
}
@@ -102,6 +112,15 @@
EXTENSIONS = []
+ ##
+ # Extensions enabled by default
+
+ DEFAULT_EXTENSIONS = [
+ :github,
+ :html,
+ :notes,
+ ]
+
##
# Creates extension methods for the `name` extension to enable and disable
# the extension and to query if they are active.
@@ -125,6 +144,11 @@
extension :css
+ ##
+ # Allow Github Flavored Markdown
+
+ extension :github
+
##
# Allow HTML
@@ -148,7 +172,10 @@
# TODO remove when kpeg 0.10 is released
alias orig_initialize initialize # :nodoc:
- def initialize extensions = [:html, :notes], debug = false
+ ##
+ # Creates a new markdown parser that enables the given +extensions+.
+
+ def initialize extensions = DEFAULT_EXTENSIONS, debug = false
@debug = debug
@formatter = RDoc::Markup::ToJoinedParagraph.new
@extensions = extensions
@@ -347,6 +374,7 @@ Doc = BOM? Block*:a { RDoc::Markup::Document.new(*a.compact) }
Block = BlankLine*
( BlockQuote
| Verbatim
+ | CodeFence
| Note
| Reference
| HorizontalRule
@@ -1009,3 +1037,14 @@ RawNoteBlock = StartList:a
( < BlankLine* > { a << text } )
{ a }
+CodeFence = &{ github? }
+ Ticks3 (Sp StrChunk:format)? Spnl < (
+ ( !"`" Nonspacechar )+ |
+ !Ticks3 "`"+ |
+ Spacechar |
+ Newline
+ )+ > Ticks3 Sp Newline*
+ { verbatim = RDoc::Markup::Verbatim.new text
+ verbatim.format = format.intern if format
+ verbatim
+ }
@@ -14,6 +14,10 @@ def initialize *parts # :nodoc:
@format = nil
end
+ def == other # :nodoc:
+ super and @format == other.format
+ end
+
##
# Calls #accept_verbatim on +visitor+
@@ -45,6 +49,21 @@ def normalize
@parts = parts
end
+ def pretty_print q # :nodoc:
+ self.class.name =~ /.*::(\w{1,4})/i
+
+ q.group 2, "[#{$1.downcase}: ", ']' do
+ if @format then
+ q.text "format: #{@format}"
+ q.breakable
+ end
+
+ q.seplist @parts do |part|
+ q.pp part
+ end
+ end
+ end
+
##
# Is this verbatim section ruby code?
View
@@ -140,6 +140,43 @@ def test_parse_code
assert_equal expected, doc
end
+ def test_parse_code_github
+ doc = parse <<-MD
+Example:
+
+```
+code goes here
+```
+ MD
+
+ expected =
+ doc(
+ para("Example:"),
+ verb("code goes here\n"))
+
+ assert_equal expected, doc
+ end
+
+ def test_parse_code_github_format
+ doc = parse <<-MD
+Example:
+
+``` ruby
+code goes here
+```
+ MD
+
+ code = verb("code goes here\n")
+ code.format = :ruby
+
+ expected =
+ doc(
+ para("Example:"),
+ code)
+
+ assert_equal expected, doc
+ end
+
def test_parse_entity_dec
doc = parse "Entity: &#65;"
@@ -2,6 +2,19 @@
class TestRDocMarkupVerbatim < RDoc::TestCase
+ def test_equals2
+ v1 = verb('1 + 1')
+ v2 = verb('1 + 1')
+ v3 = verb('1 + 2')
+ v4 = verb('1 + 1')
+ v4.format = :ruby
+
+ assert_equal v1, v2
+
+ refute_equal v1, v3
+ refute_equal v1, v4
+ end
+
def test_ruby_eh
verbatim = RDoc::Markup::Verbatim.new
View
@@ -12,6 +12,13 @@ def setup
@top_level = RDoc::TopLevel.new 'file.rb'
end
+ def mu_pp obj
+ s = ''
+ s = PP.pp obj, s
+ s = s.force_encoding Encoding.default_external if defined? Encoding
+ s.chomp
+ end
+
def test_self_encode_fallback
skip "Encoding not implemented" unless Object.const_defined? :Encoding
@@ -224,13 +231,16 @@ def test_parse_format_rd
end
def test_parse_format_tomdoc
+ code = verb('1 + 1')
+ code.format = :ruby
+
expected =
- @RM::Document.new(
- @RM::Paragraph.new('It does a thing'),
- @RM::BlankLine.new,
- @RM::Heading.new(3, 'Examples'),
- @RM::BlankLine.new,
- @RM::Verbatim.new("1 + 1"))
+ doc(
+ para('It does a thing'),
+ blank_line,
+ head(3, 'Examples'),
+ blank_line,
+ code)
text = <<-TOMDOC
It does a thing
View
@@ -216,13 +216,16 @@ def test_parse_examples
1 + 1
TEXT
+ code = verb("1 + 1\n")
+ code.format = :ruby
+
expected =
- @RM::Document.new(
- @RM::Paragraph.new('Do some stuff'),
- @RM::BlankLine.new,
- @RM::Heading.new(3, 'Examples'),
- @RM::BlankLine.new,
- @RM::Verbatim.new("1 + 1\n"))
+ doc(
+ para('Do some stuff'),
+ blank_line,
+ head(3, 'Examples'),
+ blank_line,
+ code)
document = @TD.parse(text)
assert_equal expected, document
@@ -242,21 +245,25 @@ def test_parse_examples_signature
foo(args)
TEXT
+ code1 = verb("1 + 1\n")
+ code1.format = :ruby
+
+ code2 = verb("foo(args)\n")
+
expected =
- @RM::Document.new(
- @RM::Paragraph.new('Do some stuff'),
- @RM::BlankLine.new,
- @RM::Heading.new(3, 'Examples'),
- @RM::BlankLine.new,
- @RM::Verbatim.new("1 + 1\n"),
- @RM::Heading.new(3, 'Signature'),
- @RM::BlankLine.new,
- @RM::Verbatim.new("foo(args)\n"))
+ doc(
+ para('Do some stuff'),
+ blank_line,
+ head(3, 'Examples'),
+ blank_line,
+ code1,
+ head(3, 'Signature'),
+ blank_line,
+ code2)
+
+ document = @TD.parse text
- document = @TD.parse(text)
assert_equal expected, document
- assert document.parts[4].ruby?
- refute document.parts.last.ruby?
end
def test_parse_returns

0 comments on commit ed5f3d3

Please sign in to comment.