Browse code
Saving state
fiddlerwoaroof authored on 23/05/2016 20:47:53
Showing 3 changed files
Showing 3 changed files
... | ... |
@@ -1,3 +1,3 @@ |
1 |
-- Add command line option to specify the template dir |
|
2 |
-- Make the template dir location feature more robust via distutils |
|
3 |
-- Merge my changes to pyandoc upstream |
|
1 |
+- [] Add command line option to specify the template dir |
|
2 |
+- [] Make the template dir location feature more robust via distutils |
|
3 |
+- [] Merge my changes to pyandoc upstream |
... | ... |
@@ -1,7 +1,15 @@ |
1 | 1 |
from mako.template import Template |
2 | 2 |
from mako.lookup import TemplateLookup |
3 |
+import distutils |
|
3 | 4 |
|
4 | 5 |
import argparse |
6 |
+import urllib.parse |
|
7 |
+import glob |
|
8 |
+import os |
|
9 |
+import os.path |
|
10 |
+import lxml.html |
|
11 |
+ |
|
12 |
+ |
|
5 | 13 |
parser = argparse.ArgumentParser() |
6 | 14 |
parser.add_argument('indir') |
7 | 15 |
parser.add_argument('--base', '-b', default='base.html', nargs='?') |
... | ... |
@@ -13,11 +21,6 @@ if args.odir is None: |
13 | 21 |
mylookup = TemplateLookup(directories='templates') |
14 | 22 |
template = mylookup.get_template('from-markdown.html') |
15 | 23 |
|
16 |
-import glob |
|
17 |
-import os |
|
18 |
-import os.path |
|
19 |
-import lxml.html |
|
20 |
- |
|
21 | 24 |
items = [] |
22 | 25 |
|
23 | 26 |
counter = 0 |
... | ... |
@@ -41,7 +44,7 @@ for file in glob.glob1(args.indir, '*.md'): |
41 | 44 |
title_el = '' if not title_el else title_el[0] |
42 | 45 |
|
43 | 46 |
items[-1].update( |
44 |
- link=oname, |
|
47 |
+ link=urllib.parse.quote(os.path.relpath(oname, args.odir)), |
|
45 | 48 |
counter = counter, |
46 | 49 |
title = oname if title_el is '' else title_el.text_content().strip(), |
47 | 50 |
description = desc_el if desc_el is '' else lxml.etree.tostring(desc_el).decode('utf-8') |
... | ... |
@@ -7,67 +7,66 @@ PANDOC_PATH = which('pandoc') |
7 | 7 |
|
8 | 8 |
|
9 | 9 |
class Document(object): |
10 |
- """A formatted document.""" |
|
11 |
- |
|
12 |
- INPUT_FORMATS = ( |
|
13 |
- 'native', 'markdown', 'markdown+lhs', 'rst', |
|
14 |
- 'rst+lhs', 'html', 'latex', 'latex+lhs' |
|
15 |
- ) |
|
16 |
- |
|
17 |
- OUTPUT_FORMATS = ( |
|
18 |
- 'native', 'html', 'html+lhs', 's5', 'slidy', |
|
19 |
- 'docbook', 'opendocument', 'odt', 'epub', |
|
20 |
- 'latex', 'latex+lhs', 'context', 'texinfo', |
|
21 |
- 'man', 'markdown', 'markdown+lhs', 'plain', |
|
22 |
- 'rst', 'rst+lhs', 'mediawiki', 'rtf', 'html5' |
|
23 |
- ) |
|
24 |
- |
|
25 |
- SPECIAL_FORMATS = ( |
|
26 |
- ('html+mathjax', 'html', 'mathjax'), |
|
27 |
- ('html5+mathjax', 'html5', 'mathjax'), |
|
28 |
- ) |
|
29 |
- |
|
30 |
- # TODO: Add odt, epub formats (requires file access, not stdout) |
|
31 |
- |
|
32 |
- def __init__(self): |
|
33 |
- self._content = None |
|
34 |
- self._format = None |
|
35 |
- self._register_formats() |
|
36 |
- |
|
37 |
- @classmethod |
|
38 |
- def _register_formats(cls): |
|
39 |
- """Adds format properties.""" |
|
40 |
- for fmt in cls.OUTPUT_FORMATS: |
|
41 |
- clean_fmt = fmt.replace('+', '_') |
|
42 |
- setattr(cls, clean_fmt, property( |
|
43 |
- (lambda x, fmt=fmt: cls._output(x, fmt)), # fget |
|
44 |
- (lambda x, y, fmt=fmt: cls._input(x, y, fmt)))) # fset |
|
45 |
- for fmt in cls.SPECIAL_FORMATS: |
|
46 |
- clean_fmt = fmt[0].replace('+', '_') |
|
47 |
- setattr(cls, clean_fmt, property( |
|
48 |
- (lambda x, fmt=fmt: cls._output(x, fmt[1], fmt[2])), # fget |
|
49 |
- (lambda x, y, fmt=fmt: cls._input(x, y, fmt[1], fmt[2])))) # fset |
|
50 |
- |
|
51 |
- |
|
52 |
- |
|
53 |
- def _input(self, value, format=None, modifiers=None): |
|
54 |
- # format = format.replace('_', '+') |
|
55 |
- self._content = value |
|
56 |
- self._format = format |
|
57 |
- self._modifiers = None |
|
58 |
- |
|
59 |
- def _output(self, format, modifiers=None): |
|
60 |
- # print format |
|
61 |
- if modifiers is None: |
|
62 |
- cmdline = [PANDOC_PATH, '--from=%s' % self._format, '--to=%s' % format] |
|
63 |
- else: |
|
64 |
- cmdline = [PANDOC_PATH, '--from=%s' % self._format, '--to=%s' % format, |
|
65 |
- '--%s' % modifiers] |
|
66 |
- |
|
67 |
- p = subprocess.Popen( |
|
68 |
- cmdline, |
|
69 |
- stdin=subprocess.PIPE, |
|
70 |
- stdout=subprocess.PIPE |
|
71 |
- ) |
|
72 |
- |
|
73 |
- return p.communicate(self._content)[0] |
|
10 |
+ """A formatted document.""" |
|
11 |
+ |
|
12 |
+ INPUT_FORMATS = ( |
|
13 |
+ 'native', 'markdown', 'markdown+lhs', 'rst', |
|
14 |
+ 'rst+lhs', 'html', 'latex', 'latex+lhs' |
|
15 |
+ ) |
|
16 |
+ |
|
17 |
+ OUTPUT_FORMATS = ( |
|
18 |
+ 'native', 'html', 'html+lhs', 's5', 'slidy', |
|
19 |
+ 'docbook', 'opendocument', 'odt', 'epub', |
|
20 |
+ 'latex', 'latex+lhs', 'context', 'texinfo', |
|
21 |
+ 'man', 'markdown', 'markdown+lhs', 'plain', |
|
22 |
+ 'rst', 'rst+lhs', 'mediawiki', 'rtf', 'html5' |
|
23 |
+ ) |
|
24 |
+ |
|
25 |
+ SPECIAL_FORMATS = ( |
|
26 |
+ ('html+mathjax', 'html', 'mathjax'), |
|
27 |
+ ('html5+mathjax', 'html5', 'mathjax'), |
|
28 |
+ ) |
|
29 |
+ |
|
30 |
+ # TODO: Add odt, epub formats (requires file access, not stdout) |
|
31 |
+ |
|
32 |
+ def __init__(self): |
|
33 |
+ self._content = None |
|
34 |
+ self._format = None |
|
35 |
+ self._register_formats() |
|
36 |
+ |
|
37 |
+ @classmethod |
|
38 |
+ def _register_formats(cls): |
|
39 |
+ """Adds format properties.""" |
|
40 |
+ for fmt in cls.OUTPUT_FORMATS: |
|
41 |
+ clean_fmt = fmt.replace('+', '_') |
|
42 |
+ setattr(cls, clean_fmt, property( |
|
43 |
+ (lambda x, fmt=fmt: cls._output(x, fmt)), # fget |
|
44 |
+ (lambda x, y, fmt=fmt: cls._input(x, y, fmt)))) # fset |
|
45 |
+ for fmt in cls.SPECIAL_FORMATS: |
|
46 |
+ clean_fmt = fmt[0].replace('+', '_') |
|
47 |
+ setattr(cls, clean_fmt, property( |
|
48 |
+ (lambda x, fmt=fmt: cls._output(x, fmt[1], fmt[2])), # fget |
|
49 |
+ (lambda x, y, fmt=fmt: cls._input(x, y, fmt[1], fmt[2])))) # fset |
|
50 |
+ |
|
51 |
+ |
|
52 |
+ |
|
53 |
+ def _input(self, value, format=None, modifiers=None): |
|
54 |
+ # format = format.replace('_', '+') |
|
55 |
+ self._content = value |
|
56 |
+ self._format = format |
|
57 |
+ self._modifiers = None |
|
58 |
+ |
|
59 |
+ def _output(self, format, modifiers=None): |
|
60 |
+ # print format |
|
61 |
+ if modifiers is None: |
|
62 |
+ cmdline = [PANDOC_PATH, '--from=%s' % self._format, '--to=%s' % format] |
|
63 |
+ else: |
|
64 |
+ cmdline = [PANDOC_PATH, '--from=%s' % self._format, '--to=%s' % format, '--%s' % modifiers] |
|
65 |
+ |
|
66 |
+ p = subprocess.Popen( |
|
67 |
+ cmdline, |
|
68 |
+ stdin=subprocess.PIPE, |
|
69 |
+ stdout=subprocess.PIPE |
|
70 |
+ ) |
|
71 |
+ |
|
72 |
+ return p.communicate(self._content)[0] |