Created
April 25, 2012 07:01
-
-
Save morganp/2487606 to your computer and use it in GitHub Desktop.
Maruku Snippet Generation
This file contains hidden or 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
# encoding: UTF-8 | |
require 'maruku' | |
## TODO or maybe not, we currently cut styling -- == underlining if it is on the line after the cut off length. | |
# would you want a snippet ending with a heading though? | |
def maruku_html_snippet(markdown,lines=-1 ) | |
if lines == -1 | |
markdown_temp = markdown | |
else | |
markdown_a = markdown.split("\n") | |
markdown_temp = (markdown_a[0...lines]).join("\n") | |
## Is the input text longer than cut off length | |
if markdown_a.size > lines | |
## Are we mid table ? | |
mid_table = false | |
if markdown_a[lines-1].match(/\|/) and markdown_a[lines].match(/\|/) | |
mid_table = true | |
end | |
## Looking for markdown elements we can not break (Link targets, tables) | |
(markdown_a[lines...(markdown_a.size)]).each_with_index do |text, index| | |
## Make sure keep link targets | |
if text.match(/^[ \t ]*\[[\W\w]*\]:[\W\w]*/) | |
markdown_temp << "\n" + text | |
end | |
if mid_table | |
#Still parsing table or have we finished | |
if text.match(/\|/) | |
markdown_temp << "\n" + text | |
else | |
mid_table = false | |
end | |
end | |
end | |
end | |
end | |
begin | |
html_string = Maruku.new(markdown_temp).to_html | |
html_string.gsub!("<pre>","<pre class=\"prettyprint\">") | |
rescue | |
html_string = 'Post rendering failed, possibly due to an illegal charater' | |
end | |
return html_string | |
end | |
if $0 == __FILE__ | |
require 'pp' | |
basic_md = "line1 \n[line2][] \nline3 \nline4 \nline5 \n[line2]: http://google.co.uk" | |
puts basic_md | |
# All of basic_md | |
puts | |
puts maruku_html_snippet(basic_md, 7) | |
puts | |
puts maruku_html_snippet(basic_md, 3) | |
tables_md = %{ | |
mmm Tables | |
== | |
Col1 | Col2 | |
--|:-- | |
dat1|dat2 | |
} | |
# All of basic_md | |
puts | |
puts maruku_html_snippet(tables_md) | |
puts | |
puts maruku_html_snippet(tables_md, 3) | |
puts | |
puts maruku_html_snippet(tables_md, 5) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment