How can I convert github flavored markdown to HTML?
Asked Answered
A

3

7

I know github has released the Redcarpet gem for converting markdown to HTML but as far as I have seen it doesn't support (or recognize) Github flavored markdown such as

javascript var x = 1;

Anyone know if there is a gem (or some way with redcarpet) to handle the github flavored syntax, specifically I am interested in the syntax highlighting.

Thanks.

Amalle answered 14/3, 2012 at 0:23 Comment(0)
S
4

Now better to use github-markdown gem.

GitHub::Markdown.render(content)
Sprit answered 16/4, 2012 at 17:3 Comment(1)
thanks for the comment, glad to see this gem is available and being updated.Amalle
S
3

You can use Redcarpet for converting markdown code to HTML. Here you have two examples extracted from Redcarpet project tests

def test_compat_api_knows_fenced_code_extension
  text = "```ruby\nx = 'foo'\n```"
  html = RedcarpetCompat.new(text, :fenced_code).to_html
  html_equal "<pre><code class=\"ruby\">x = 'foo'\n</code></pre>", html
end

def test_compat_api_ignores_gh_blockcode_extension
  text = "```ruby\nx = 'foo'\n```"
  html = RedcarpetCompat.new(text, :fenced_code, :gh_blockcode).to_html
  html_equal "<pre><code class=\"ruby\">x = 'foo'\n</code></pre>", html
end

I hope this answers your question

Surgery answered 5/4, 2012 at 12:24 Comment(0)
N
0

GitHub publishes the github-markup gem, which can convert markup to HTML. You can either install it as a gem:

$ gem install github-markup

Or, if you are running Ubuntu, install it using apt:

$ sudo apt install ruby-github-markup

Github-markup can be used in a Ruby program and from the command line.

Ruby Program Usage

Unfortunately, the code examples of how to use github-markup that are shown in the README are under-documented. However, the command line source code is a complete working example of how to use github-markup in a Ruby program.

#!/usr/bin/env ruby

$LOAD_PATH.unshift File.dirname(File.realpath(__FILE__)) + "/../lib"
require 'github/markup'

if ARGV.size < 1
  print "usage: #{File.basename($0)} FILE [ FILES ... ]\n"
  exit 1
end

sources = []

ARGV.each { |s|
  begin
    file = File.open( s, "r" )
    sources.push [ s, file ]
  rescue Exception => e
    $stderr.print "error: #{e.message}\n"
    exit 1
  ensure
  end
}

sources.each { |name, file|
  print GitHub::Markup.render( name, file.read )
  file.close
}

Command Line Usage

Here is an example of using github-markup in a command line:

$ github-markup README.md

Copying HTML Output to the System Clipboard

I normally work in WSL. Here is a convenient way to copy the generated HTML to the clipboard:

$ github-markup README.md | clip.exe

On a generic X11 terminal, you could accomplish the same thing using xclip:

$ alias xclip='xclip -selection clipboard'
$ github-markup README.md | xclip

Many other Linux clipboard utilities are available, for example cb.

Shelling Out From Ruby

You could also run the command-line executable in a Ruby program:

puts `github-markup README.md`.chomp

Shelling Out From Python

You could also run the command-line executable in a Python program:

import os
print(os.system('github-markup README.md'))
Niggardly answered 2/12, 2023 at 12:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.