Skip to content

Instantly share code, notes, and snippets.

@defHLT
Last active September 11, 2017 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save defHLT/80312907590514526f76dad61bb1b21e to your computer and use it in GitHub Desktop.
Save defHLT/80312907590514526f76dad61bb1b21e to your computer and use it in GitHub Desktop.
Parser from markdown to wordpress for RayWenderlich website
#!/usr/bin/ruby
require 'redcarpet'
class RayParser < Redcarpet::Render::HTML
# *important concept*
def emphasis(text)
"<i>#{text}</i>"
end
# filenames, menus. e.g.: **App.java**
def double_emphasis(text)
"<em>#{text}</em>"
end
def header(text, level)
case level
when 1
"<h2>#{text}</h2>\n"
when 2
"<h3>#{text}</h3>\n"
when 3
"<em>#{text}</em>\n"
else
throw "Unsupported header level #{level}"
end
end
def block_code(code, language)
"<pre lang=\"#{language}\">\n#{code}</pre>"
end
# Wordpress will insert <p> tags
def paragraph(text)
"\n#{text}\n"
end
# Wordpress will escape ampersands and single quotes
def postprocess(doc)
doc.gsub("&amp;", ?&)
.gsub("&#39;", ?')
end
end
if ARGV.size != 1
puts "Usage: #{$PROGRAM_NAME} file.md"
exit
end
markdown = Redcarpet::Markdown.new(RayParser, extensions = { fenced_code_blocks: true, disable_indented_code_blocks: true } )
text = File.read(ARGV[0])
puts markdown.render(text)
@defHLT
Copy link
Author

defHLT commented Aug 30, 2016

Example text:

# Header 1

## Header 2

### Header 3

A paragraph

1. one
2. two
3. three

- one
- two
- three

*Important* concept
**File.java**

Output:

 % ./parser.rb test.md
<h2>Header 1</h2>
<h3>Header 2</h3>
<em>Header 3</em>

A paragraph

<ol>
<li>one</li>
<li>two</li>
<li>three</li>
</ol>

<ul>
<li>one</li>
<li>two</li>
<li>three</li>
</ul>

<i>Important</i> concept
<em>File.java</em>

@defHLT
Copy link
Author

defHLT commented Aug 30, 2016

Code blocks:

```java
code
```

Output:

<pre lang="java">
code
</pre>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment