forked from gettalong/kramdown
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create a GFM (Github Flavored Markdown) parser
As a first step, it supports "fenced code blocks" delimited by three or more backticks.
- Loading branch information
Showing
6 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
--- | ||
title: GFM Parser | ||
--- | ||
# GFM Parser | ||
|
||
## Introduction | ||
|
||
Parse ["Github Flavored Markdown"](https://help.github.com/articles/github-flavored-markdown). This is a format of Markdown that is used on Github.com for most places where textual input is required, such as issues and comments. Some of the extensions, notably the "backtick fenced code blocks" are also used on other sites, for example StackOverflow. | ||
|
||
## Conformance | ||
|
||
At the moment this parser is identical to the Kramdown parser, except that it adds support for fenced code blocks using three or more backticks to delimit the block. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
module Kramdown | ||
module Parser | ||
class GFM < Kramdown::Parser::Kramdown | ||
|
||
def initialize(source, options) | ||
super | ||
@block_parsers.unshift(:gfm_codeblock_fenced) | ||
end | ||
|
||
GFM_FENCED_CODEBLOCK_START = /^`{3,}/ | ||
#GFM_FENCED_CODEBLOCK_MATCH = /^(`{3,})\s*?(\w+)?\s*?\n(.*?)^\1`*\s*?\n/m | ||
GFM_FENCED_CODEBLOCK_MATCH = /^`{3,} *(\w+)?\s*?\n(.*?)^`{3,}\s*?\n/m | ||
|
||
# Parse the fenced codeblock at the current location. | ||
def parse_gfm_codeblock_fenced | ||
if @src.check(GFM_FENCED_CODEBLOCK_MATCH) | ||
@src.pos += @src.matched_size | ||
el = new_block_el(:codeblock, @src[2]) | ||
lang = @src[1].to_s.strip | ||
el.attr['class'] = "language-#{lang}" unless lang.empty? | ||
@tree.children << el | ||
true | ||
else | ||
false | ||
end | ||
end | ||
define_parser(:gfm_codeblock_fenced, GFM_FENCED_CODEBLOCK_START) | ||
|
||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<pre><code>Three backticks | ||
</code></pre> | ||
|
||
<pre><code>Four backticks | ||
</code></pre> | ||
|
||
<pre><code>Unbalanced bottom heavy | ||
</code></pre> | ||
|
||
<pre><code>Unbalanced top heavy | ||
</code></pre> | ||
|
||
<div><div class="CodeRay"> | ||
<div class="code"><pre><span class="line-numbers"><a href="#n1" name="n1">1</a></span>language no space | ||
</pre></div> | ||
</div> | ||
</div> | ||
|
||
<div><div class="CodeRay"> | ||
<div class="code"><pre><span class="line-numbers"><a href="#n1" name="n1">1</a></span>language with space | ||
</pre></div> | ||
</div> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
``` | ||
Three backticks | ||
``` | ||
|
||
```` | ||
Four backticks | ||
```` | ||
|
||
``` | ||
Unbalanced bottom heavy | ||
`````` | ||
|
||
`````` | ||
Unbalanced top heavy | ||
```` | ||
|
||
````ruby | ||
language no space | ||
```` | ||
|
||
```` ruby | ||
language with space | ||
```` |