Skip to content

Instantly share code, notes, and snippets.

@jiafulow
Last active August 29, 2015 13:57
Show Gist options
  • Save jiafulow/9842241 to your computer and use it in GitHub Desktop.
Save jiafulow/9842241 to your computer and use it in GitHub Desktop.
gfm2html.py
#!/usr/bin/env python
import sys
import urllib2
import json
# To learn GitHub Flavored Markdown, see
# http://guides.github.com/overviews/mastering-markdown
gh_url = 'https://api.github.com/markdown'
def post(text, mode='gfm', context=None):
if mode == "mistune":
import mistune
response = mistune.markdown(text, hard_wrap=True)
else:
data = json.dumps({'text': text, 'mode': mode, 'context': context})
#values = {'text': text, 'mode': mode, 'context': context}
headers = {'content-type': 'application/json'}
#headers = {'content-type': 'text/plain'}
request = urllib2.Request(gh_url, data=data, headers=headers)
f = urllib2.urlopen(request)
response = f.read()
f.close()
return response
def main():
# open the input markdown file
try:
f = open(sys.argv[1], 'r')
text = f.read()
f.close()
except:
print "Error: cannot open the input markdown file."
# open the input css file
# the default css file comes from
# https://github.com/jasonm23/markdown-css-themes/blob/gh-pages/swiss.css
# with syntax highlight added from
# https://github.com/jekyll/jekyll/blob/master/lib/site_template/css/main.css
cssfile = "swiss_hl.css"
if len(sys.argv) > 2 and sys.argv[2].endswith(".css"):
cssfile = sys.argv[2]
try:
f = open(cssfile, 'r')
css = f.read()
f.close()
except:
print "Error: cannot open the input css file."
# output
html = """
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">%s</style>
</head>
<body>
<div class="wrapper">%s</div>
</body>
</html>
"""
#response = post(text, mode="gfm")
response = post(text, mode="mistune")
print html % (css, response)
if __name__ == "__main__":
if len(sys.argv) <= 1:
print "Usage: python %s main.md [main.css]" % sys.argv[0]
sys.exit(1)
main()
@charset "utf-8";
/**
* markdown.css
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see http://gnu.org/licenses/lgpl.txt.
*
* @project Weblog and Open Source Projects of Florian Wolters
* @version GIT: $Id$
* @package xhtml-css
* @author Florian Wolters <[email protected]>
* @copyright 2012 Florian Wolters
* @cssdoc version 1.0-pre
* @license http://gnu.org/licenses/lgpl.txt GNU Lesser General Public License
* @link http://github.com/FlorianWolters/jekyll-bootstrap-theme
* @media all
* @valid true
*/
body {
font-family: Helvetica, Arial, Freesans, clean, sans-serif;
padding:1em;
margin:auto;
max-width:60em;
background:#fefefe;
}
h1, h2, h3, h4, h5, h6 {
font-weight: bold;
}
h1 {
color: #000000;
font-size: 28px;
}
h2 {
border-bottom: 1px solid #CCCCCC;
color: #000000;
font-size: 24px;
}
h3 {
font-size: 18px;
}
h4 {
font-size: 16px;
}
h5 {
font-size: 14px;
}
h6 {
color: #777777;
background-color: inherit;
font-size: 14px;
}
hr {
height: 0.2em;
border: 0;
color: #CCCCCC;
background-color: #CCCCCC;
}
p, blockquote, ul, ol, dl, li, table, pre {
margin: 15px 0;
}
code, pre {
border-radius: 3px;
background-color: #F8F8F8;
color: inherit;
}
code {
border: 1px solid #EAEAEA;
margin: 0 2px;
padding: 0 5px;
}
pre {
border: 1px solid #CCCCCC;
line-height: 1.25em;
overflow: auto;
padding: 6px 10px;
}
pre > code {
border: 0;
margin: 0;
padding: 0;
}
a, a:visited {
color: #4183C4;
background-color: inherit;
text-decoration: none;
}
/* Syntax highlighting styles */
/* ----------------------------------------------------------*/
.highlight { background: #ffffff; }
.highlight .c { color: #999988; font-style: italic } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { font-weight: bold } /* Keyword */
.highlight .o { font-weight: bold } /* Operator */
.highlight .cm { color: #999988; font-style: italic } /* Comment.Multiline */
.highlight .cp { color: #999999; font-weight: bold } /* Comment.Preproc */
.highlight .c1 { color: #999988; font-style: italic } /* Comment.Single */
.highlight .cs { color: #999999; font-weight: bold; font-style: italic } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .gd .x { color: #000000; background-color: #ffaaaa } /* Generic.Deleted.Specific */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #999999 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .gi .x { color: #000000; background-color: #aaffaa } /* Generic.Inserted.Specific */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #aaaaaa } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { font-weight: bold } /* Keyword.Constant */
.highlight .kd { font-weight: bold } /* Keyword.Declaration */
.highlight .kp { font-weight: bold } /* Keyword.Pseudo */
.highlight .kr { font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #445588; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #009999 } /* Literal.Number */
.highlight .s { color: #d14 } /* Literal.String */
.highlight .na { color: #008080 } /* Name.Attribute */
.highlight .nb { color: #0086B3 } /* Name.Builtin */
.highlight .nc { color: #445588; font-weight: bold } /* Name.Class */
.highlight .no { color: #008080 } /* Name.Constant */
.highlight .ni { color: #800080 } /* Name.Entity */
.highlight .ne { color: #990000; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #990000; font-weight: bold } /* Name.Function */
.highlight .nn { color: #555555 } /* Name.Namespace */
.highlight .nt { color: #000080 } /* Name.Tag */
.highlight .nv { color: #008080 } /* Name.Variable */
.highlight .ow { font-weight: bold } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mf { color: #009999 } /* Literal.Number.Float */
.highlight .mh { color: #009999 } /* Literal.Number.Hex */
.highlight .mi { color: #009999 } /* Literal.Number.Integer */
.highlight .mo { color: #009999 } /* Literal.Number.Oct */
.highlight .sb { color: #d14 } /* Literal.String.Backtick */
.highlight .sc { color: #d14 } /* Literal.String.Char */
.highlight .sd { color: #d14 } /* Literal.String.Doc */
.highlight .s2 { color: #d14 } /* Literal.String.Double */
.highlight .se { color: #d14 } /* Literal.String.Escape */
.highlight .sh { color: #d14 } /* Literal.String.Heredoc */
.highlight .si { color: #d14 } /* Literal.String.Interpol */
.highlight .sx { color: #d14 } /* Literal.String.Other */
.highlight .sr { color: #009926 } /* Literal.String.Regex */
.highlight .s1 { color: #d14 } /* Literal.String.Single */
.highlight .ss { color: #990073 } /* Literal.String.Symbol */
.highlight .bp { color: #999999 } /* Name.Builtin.Pseudo */
.highlight .vc { color: #008080 } /* Name.Variable.Class */
.highlight .vg { color: #008080 } /* Name.Variable.Global */
.highlight .vi { color: #008080 } /* Name.Variable.Instance */
.highlight .il { color: #009999 } /* Literal.Number.Integer.Long */
/* Text highlighting styles */
/* ----------------------------------------------------------*/
.label {display: inline-block; padding: 3px 4px; font-size: 85%; font-weight: bold; background-color: #ccc; color: #000; border: 1px solid transparent; border-radius: 2px; text-align: center;}
.label:hover {text-decoration: none}
.label-yellow {background-color: #fbca04 !important; color: #332900 !important;}
.label-gray {background-color: #68645b !important; color: #f5f5f5 !important;}
.label-lightgray {background-color: #e9e9e9 !important; color: #555 !important;}
.label-lightblue {background-color: #d8ebf8 !important; color: #2a65a0 !important;}
.label-dark1 {background-color: #e11d20 !important; color: #fff !important;}
.label-dark2 {background-color: #fa6420 !important; color: #fff !important;}
.label-dark3 {background-color: #eeb500 !important; color: #fff !important;}
.label-dark4 {background-color: #009800 !important; color: #fff !important;}
.label-dark5 {background-color: #006b75 !important; color: #fff !important;}
.label-dark6 {background-color: #2080e5 !important; color: #fff !important;}
.label-dark7 {background-color: #0039bf !important; color: #fff !important;}
.label-dark8 {background-color: #5319e7 !important; color: #fff !important;}
.label-dark9 {background-color: #d82a8d !important; color: #fff !important;}
.label-light1 {background-color: #f7c6c7 !important; color: #332c2c !important;}
.label-light2 {background-color: #fad8c7 !important; color: #332c2c !important;}
.label-light3 {background-color: #fae5a0 !important; color: #333028 !important;}
.label-light4 {background-color: #bfe5bf !important; color: #2a332a !important;}
.label-light5 {background-color: #bfdadc !important; color: #2a332a !important;}
.label-light6 {background-color: #c7def8 !important; color: #2c2c33 !important;}
.label-light7 {background-color: #bfd0f5 !important; color: #2c2c33 !important;}
.label-light8 {background-color: #d4c5f9 !important; color: #2c2c33 !important;}
.label-light9 {background-color: #fac4e3 !important; color: #2c2c33 !important;}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment