markdown viewer script for your markdown documents

Update

Made the script more awesome by adding code highlighting using pygments. Download the pygments file from https://substancehq.s3.amazonaws.com/assets/pygments-18ba01c79ccfbe022848bccbb4413b59.css , and the markdown stylesheet from https://substancehq.s3.amazonaws.com/static_asset/4fbd972603b04d30730006da/markdown.css


Here is a script which I use for reading my markdown documents in the browser. It uses css from https://github.com/clownfart/Markdown-CSS. Here is a sample page with that css http://planetzxy.com/markdown/. And here is the script:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55

#~/.scripts/markdown-viewer

#!/usr/bin/env ruby
require 'rubygems'
require 'redcarpet'
require 'erb'
require 'pygments'

class CustomHtmlRenderer < Redcarpet::Render::HTML
def block_code(code, lang)
Pygments.highlight code, :lexer => lang
end
end

#@markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML, :autolink => true)
@markdown = Redcarpet::Markdown.new(CustomHtmlRenderer, autolink: true, fenced_code_blocks: true, tables: true, no_intra_emphasis: true, strikethrough: true, superscript: true)


@template = ERB.new(<<EOF
<!DOCTYPE html>
<html>
<head>
<title><%= title %></title>
<link href="https://raw.github.com/clownfart/Markdown-CSS/master/markdown.css" rel="stylesheet"></link>
</head>
<body>
<%= body %>
</body>
</html>
EOF
)

def view_markdown(filename)

if !File.exists?(filename)
puts "#{filename} does not exist."
end

opfilename = "/home/minhajuddin/tmp/mdrendered/#{File.basename(filename)}.html"
File.open(opfilename, 'w') do |f|
file_content = File.read(filename)
title = file_content.lines.first.chomp
body = @markdown.render(file_content)
f.write @template.result(binding)
end

system("gnome-open #{opfilename}")

end

ARGV.each do |arg|
view_markdown(arg)
end


I am currently working on LiveForm which makes setting up contact forms on your website a breeze.