1
2#!/usr/bin/env python3
3
4############################################################################
5#
6# MODULE:       mkrest.py
7# AUTHOR(S):    Luca Delucchi
8# PURPOSE:      Create HTML manual page snippets
9# COPYRIGHT:    (C) 2012 by Luca Delucchi
10#                and the GRASS Development Team
11#
12#               This program is free software under the GNU General
13#               Public License (>=v2). Read the file COPYING that
14#               comes with GRASS for details.
15#
16#############################################################################
17
18import sys
19import string
20import re
21import subprocess
22from datetime import datetime
23
24pgm = sys.argv[1]
25if len(sys.argv) > 1:
26    year = sys.argv[2]
27else:
28    year = str(datetime.now().year)
29
30src_file = "%s.html" % pgm
31tmp_file = "%s.tmp.txt" % pgm
32
33#TODO add copyright
34
35footer_index = string.Template(\
36"""
37
38:doc:`Main Page <index>` - :doc:`${INDEXNAMECAP} index <${INDEXNAME}>` - :doc:`Full index <full_index>`
392003-${YEAR} `GRASS Development Team <https://grass.osgeo.org>`_
40""")
41
42footer_noindex = string.Template(\
43"""
44
45:doc:`Main Page <index>`  - :doc:`Full index <full_index>`
462003-${YEAR} `GRASS Development Team <https://grass.osgeo.org>`_
47""")
48
49
50def read_file(name):
51    try:
52        f = open(name, 'rb')
53        s = f.read()
54        f.close()
55        return s
56    except IOError:
57        return ""
58
59replacement = {
60    '*`' : '`',
61    '`* `' : '`',
62    '>`_*' : '>`_',
63    '>`_,*' : '>`_,',
64    '``*\ "' : '``"',
65    '***' : '**'
66}
67
68src_data = read_file(src_file)
69
70title = re.search('(<!-- meta page description:)(.*)(-->)', src_data, re.IGNORECASE)
71
72if title:
73    title_name = title.group(2).strip()
74    sys.stdout.write("%s\n" % title_name)
75    title_style = "=" * (len(title_name)+2)
76    sys.stdout.write("%s\n\n" % title_style)
77
78tmp_data = read_file(tmp_file)
79if tmp_data:
80    sys.stdout.write(tmp_data)
81
82process = subprocess.Popen('pandoc -s -r html %s -w rst' % src_file,
83                           shell=True, stdout=subprocess.PIPE)
84html_text = process.communicate()[0]
85if html_text:
86    for k, v in replacement.iteritems():
87        html_text = html_text.replace(k, v)
88
89#TODO remove with space if string start with it, " vector..." -> "vector..."
90#     not if it is a tab: "     vector...." -> "    vector..."
91
92#    for line in html_text.splitlines(True):
93#        sys.stdout.write("%s" % line.lstrip())
94
95sys.stdout.write(html_text)
96
97index_names = {
98    'd': 'display',
99    'db': 'database',
100    'g': 'general',
101    'i': 'imagery',
102    'm': 'miscellaneous',
103    'ps': 'postscript',
104    'p': 'paint',
105    'r': 'raster',
106    'r3': 'raster3D',
107    's': 'sites',
108    't': 'temporal',
109    'v': 'vector'
110    }
111
112index = re.search('(<!-- meta page index:)(.*)(-->)', src_data, re.IGNORECASE)
113
114if index:
115    index_name = index.group(2).strip()
116else:
117    mod_class = pgm.split('.', 1)[0]
118    index_name = index_names.get(mod_class, '')
119
120if index_name:
121    sys.stdout.write(footer_index.substitute(INDEXNAME = index_name,
122                                             INDEXNAMECAP = index_name.title(),
123                                             YEAR = year))
124else:
125    sys.stdout.write(footer_noindex.substitute(YEAR = year))
126
127sys.exit()
128