Browse code
Basically complete now.
- globs for markdown files (*.md)
- generates index.html
- basic theming support: you can specify an alternate base template
Showing 9 changed files
- .gitignore
- blog-gen/__main__.py
- processed_README.html
- example_output/TODO.html
- example_output/index.html
- initialize.sh
- templates/base.html
- templates/from-markdown.html
- templates/index.html
... | ... |
@@ -3,12 +3,51 @@ from mako.lookup import TemplateLookup |
3 | 3 |
|
4 | 4 |
import argparse |
5 | 5 |
parser = argparse.ArgumentParser() |
6 |
-parser.add_argument('infile') |
|
6 |
+parser.add_argument('indir') |
|
7 | 7 |
parser.add_argument('--base', '-b', default='base.html', nargs='?') |
8 |
+parser.add_argument('--odir', '-o') |
|
8 | 9 |
args = parser.parse_args() |
10 |
+if args.odir is None: |
|
11 |
+ args.odir = args.indir |
|
9 | 12 |
|
10 | 13 |
mylookup = TemplateLookup(directories='templates') |
11 | 14 |
template = mylookup.get_template('from-markdown.html') |
12 | 15 |
|
13 |
-with open(args.infile) as f: |
|
14 |
- print(template.render(content=f.read(), base=args.base)) |
|
16 |
+import glob |
|
17 |
+import os |
|
18 |
+import os.path |
|
19 |
+import lxml.html |
|
20 |
+ |
|
21 |
+items = [] |
|
22 |
+ |
|
23 |
+counter = 0 |
|
24 |
+for file in glob.glob1(args.indir, '*.md'): |
|
25 |
+ oname = os.path.join(args.odir, '%s.html' % file.rpartition('.')[0]) |
|
26 |
+ |
|
27 |
+ with open(os.path.join(args.indir, file)) as f: |
|
28 |
+ out = template.render(content=f.read(), base=args.base) |
|
29 |
+ with open(oname, 'wb') as of: |
|
30 |
+ of.write(out.encode('utf-8')) |
|
31 |
+ |
|
32 |
+ counter += 1 |
|
33 |
+ items.append({}) |
|
34 |
+ htmldoc = lxml.html.fromstring(out) |
|
35 |
+ |
|
36 |
+ desc_el = htmldoc.xpath('//div[contains(@class,"container") and contains(@class, "has_description")]/p[1]') |
|
37 |
+ print(desc_el) |
|
38 |
+ desc_el = '' if not desc_el else desc_el[0] |
|
39 |
+ |
|
40 |
+ title_el = htmldoc.xpath('//h1 | //h2 | //h3 | //h4 | //h4 | //h6') |
|
41 |
+ title_el = '' if not title_el else title_el[0] |
|
42 |
+ |
|
43 |
+ items[-1].update( |
|
44 |
+ link=oname, |
|
45 |
+ counter = counter, |
|
46 |
+ title = oname if title_el is '' else title_el.text_content().strip(), |
|
47 |
+ description = desc_el if desc_el is '' else lxml.etree.tostring(desc_el).decode('utf-8') |
|
48 |
+ ) |
|
49 |
+ |
|
50 |
+index_template = mylookup.get_template('index.html') |
|
51 |
+index = index_template.render(items=items, base=args.base) |
|
52 |
+with open(os.path.join(args.odir, 'index.html'), 'wb') as of: |
|
53 |
+ of.write(index.encode('utf-8')) |
15 | 54 |
similarity index 95% |
16 | 55 |
rename from processed_README.html |
17 | 56 |
rename to example_output/README.html |
... | ... |
@@ -7,18 +7,16 @@ |
7 | 7 |
|
8 | 8 |
|
9 | 9 |
<title></title> |
10 |
- <style type="text/css">code{white-space: pre;}</style> |
|
11 | 10 |
|
12 | 11 |
<!--[if lt IE 9]> |
13 | 12 |
<style type="text/css">code{white-space: pre;}</style> |
14 |
- <!--[if lt IE 9]> |
|
15 | 13 |
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script> |
16 | 14 |
<![endif]--> |
17 | 15 |
<script src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML" type="text/javascript"></script> |
18 | 16 |
|
19 | 17 |
</head> |
20 | 18 |
<body> |
21 |
- <div class="container"> |
|
19 |
+ <div class="container has_description"> |
|
22 | 20 |
|
23 | 21 |
|
24 | 22 |
|
... | ... |
@@ -26,7 +24,7 @@ |
26 | 24 |
<p>This is a relatively simple blog generator that uses mako and pandoc to produce html pages with MathJaX includes.</p> |
27 | 25 |
<h3 id="usage">Usage</h3> |
28 | 26 |
<pre><code>python3 -m blog-gen <infile> [-b <base-template>]</code></pre> |
29 |
-<p>The input file is any old pandoc markdown file. The base template is specified relative to the template lookup's root: it defaults to "base.html". Running this command will spit out a static page that contains the rendered markdown. The cool thing about this is that pandoc will convert Latex math expressions like <code>$x_2 + \frac{1}{2}x_{1}^{2}$</code> into the MathJaX equivalent: if you run this file through the blog generator, you'd get: <span class="math">\(x_2 + \frac{1}{2}x_{1}^{2}\)</span></p> |
|
27 |
+<p>The input file is any old pandoc markdown file. The base template is specified relative to the template lookup's root: it defaults to "base.html". Running this command will spit out a static page that contains the rendered markdown. The cool thing about this is that pandoc will convert Latex math expressions like <code>$x_2 + \frac{1}{2}x_{1}^{2}$</code> into the MathJaX equivalent: if you run this file through the blog generator, you'd get: <span class="math">\(x_2 + \frac{1}{2}x_{1}^{2}\)</span> (for example, see [raw/master/processed_README.html])</p> |
|
30 | 28 |
<h3 id="installation">Installation</h3> |
31 | 29 |
<h4 id="prerequisites">Prerequisites</h4> |
32 | 30 |
<p>The only prerequisite that cannot be installed with pip is pandoc, which can either be installed via <code>cabal install</code> or from your distribution's package archives.</p> |
... | ... |
@@ -53,4 +51,3 @@ |
53 | 51 |
</div> |
54 | 52 |
</body> |
55 | 53 |
</html> |
56 |
- |
57 | 54 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,33 @@ |
1 |
+<!DOCTYPE html> |
|
2 |
+<html> |
|
3 |
+<head> |
|
4 |
+ <meta charset="utf-8"> |
|
5 |
+ <meta name="generator" content="blog-gen"> |
|
6 |
+ <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes"> |
|
7 |
+ |
|
8 |
+ |
|
9 |
+ <title></title> |
|
10 |
+ |
|
11 |
+ <!--[if lt IE 9]> |
|
12 |
+ <style type="text/css">code{white-space: pre;}</style> |
|
13 |
+ <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script> |
|
14 |
+ <![endif]--> |
|
15 |
+ <script src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML" type="text/javascript"></script> |
|
16 |
+ |
|
17 |
+</head> |
|
18 |
+<body> |
|
19 |
+ <div class="container has_description"> |
|
20 |
+ |
|
21 |
+ |
|
22 |
+ |
|
23 |
+<ul> |
|
24 |
+<li>Add command line option to specify the template dir</li> |
|
25 |
+<li>Make the template dir location feature more robust via distutils</li> |
|
26 |
+<li>Merge my changes to pyandoc upstream</li> |
|
27 |
+</ul> |
|
28 |
+ |
|
29 |
+ |
|
30 |
+ |
|
31 |
+ </div> |
|
32 |
+</body> |
|
33 |
+</html> |
0 | 34 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,27 @@ |
1 |
+<!DOCTYPE html> |
|
2 |
+<html> |
|
3 |
+<head> |
|
4 |
+ <meta charset="utf-8"> |
|
5 |
+ <meta name="generator" content="blog-gen"> |
|
6 |
+ <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes"> |
|
7 |
+ |
|
8 |
+ <title></title> |
|
9 |
+ |
|
10 |
+</head> |
|
11 |
+<body> |
|
12 |
+ <div class="container has_description"> |
|
13 |
+ |
|
14 |
+ <h1>Blog Index</h1> |
|
15 |
+ <article id="1"> |
|
16 |
+ <h2><a href="example_output/README.html">My simplistic blogfile generator</a></h2> |
|
17 |
+ <p class="description"><p>This is a relatively simple blog generator that uses mako and pandoc to produce html pages with MathJaX includes.</p> |
|
18 |
+</p> |
|
19 |
+ </article> |
|
20 |
+ <article id="2"> |
|
21 |
+ <h2><a href="example_output/TODO.html">example_output/TODO.html</a></h2> |
|
22 |
+ <p class="description"></p> |
|
23 |
+ </article> |
|
24 |
+ |
|
25 |
+ </div> |
|
26 |
+</body> |
|
27 |
+</html> |
... | ... |
@@ -6,11 +6,10 @@ |
6 | 6 |
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes"> |
7 | 7 |
<%block name="header"> |
8 | 8 |
<title></title> |
9 |
- <style type="text/css">code{white-space: pre;}</style> |
|
10 | 9 |
</%block> |
11 | 10 |
</head> |
12 | 11 |
<body> |
13 |
- <div class="container"> |
|
12 |
+ <div class="container has_description"> |
|
14 | 13 |
<%block name="content_block" /> |
15 | 14 |
</div> |
16 | 15 |
</body> |
... | ... |
@@ -5,7 +5,6 @@ |
5 | 5 |
${parent.header()} |
6 | 6 |
<!--[if lt IE 9]> |
7 | 7 |
<style type="text/css">code{white-space: pre;}</style> |
8 |
- <!--[if lt IE 9]> |
|
9 | 8 |
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script> |
10 | 9 |
<![endif]--> |
11 | 10 |
<script src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML" type="text/javascript"></script> |
12 | 11 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,14 @@ |
1 |
+<%inherit file="${context['base']}" /> |
|
2 |
+<%namespace file="pandoc.mako" name="pandoc" /> |
|
3 |
+ |
|
4 |
+<%block name="title">Blog Index</%block> |
|
5 |
+ |
|
6 |
+<%block name="content_block"> |
|
7 |
+ <h1>Blog Index</h1> |
|
8 |
+ %for item in items: |
|
9 |
+ <article id="${item['counter']}"> |
|
10 |
+ <h2><a href="${item['link']}">${item['title']}</a></h2> |
|
11 |
+ <p class="description">${item['description']}</p> |
|
12 |
+ </article> |
|
13 |
+ %endfor |
|
14 |
+</%block> |