Debian Codegraph @

doc/runrst

raw
1 #!/usr/bin/env python3
2 #
3 # runrst - register custom roles and run correct writer
4 #
5 # Copyright 2010 Olivia Mackall <[email protected]> and others
6 #
7 # This software may be used and distributed according to the terms of the
8 # GNU General Public License version 2 or any later version.
9
10 """usage: %s WRITER args...
11
12 where WRITER is the name of a Docutils writer such as 'html' or 'manpage'
13 """
14
15
16 import re
17 import sys
18
19 try:
20 import docutils.core as core
21 import docutils.nodes as nodes
22 import docutils.utils as utils
23 import docutils.parsers.rst.roles as roles
24 except ImportError:
25 sys.stderr.write(
26 "abort: couldn't generate documentation: docutils "
27 "module is missing\n"
28 )
29 sys.stderr.write(
30 "please install python-docutils or see "
31 "http://docutils.sourceforge.net/\n"
32 )
33 sys.exit(-1)
34
35 # Whether we are rendering a help page for a single topic.
36 # If false, we are rendering a monolithic page with all topics together.
37 is_individual_pages_mode = False
38
39
40 def make_cmd_ref_uri(cmd):
41 if is_individual_pages_mode:
42 return "hg-%s.html" % cmd
43 else:
44 return "hg.1.html#%s" % cmd
45
46
47 known_refs = None
48
49
50 def load_known_refs(fname):
51 try:
52 with open(fname, 'r') as fp:
53 text = fp.read()
54 return re.split(r'[ \n]+', text)
55 except OSError:
56 sys.stderr.write(
57 "abort: couldn't find '%', please run documentation generation "
58 "through the Makefile, or run 'make knownrefs'\n"
59 )
60 sys.exit(-1)
61
62
63 def find_known_ref(ref):
64 global known_refs
65 if known_refs is None:
66 cmds = load_known_refs('commandlist.txt')
67 topics = load_known_refs('topiclist.txt')
68 exts = load_known_refs('extensionlist.txt')
69 known_refs = {'hg': cmds, 'topic': topics, 'ext': exts}
70 for reftype, refnames in known_refs.items():
71 if ref in refnames:
72 return reftype
73 return None
74
75
76 def make_any_ref_uri(ref):
77 if is_individual_pages_mode:
78 # Try to find if ref is a command, topic, or extension. If not,
79 # reference the anchor in the main hg.1 help page.
80 reftype = find_known_ref(ref)
81 if reftype:
82 return '%s-%s.html' % (reftype, ref)
83 return "hg.1.html#%s" % ref
84
85
86 def role_hg(name, rawtext, text, lineno, inliner, options=None, content=None):
87 text = "hg " + utils.unescape(text)
88 linktext = nodes.literal(rawtext, text)
89 parts = text.split()
90 cmd, args = parts[1], parts[2:]
91 refuri = make_cmd_ref_uri(cmd)
92 if cmd == 'help' and args:
93 if args[0] == 'config':
94 # :hg:`help config`
95 refuri = "hgrc.5.html"
96 elif args[0].startswith('config.'):
97 # :hg:`help config.SECTION...`
98 refuri = "hgrc.5.html#%s" % args[0].split('.', 2)[1]
99 elif len(args) >= 2 and args[0] == '-c':
100 # :hg:`help -c COMMAND ...` is equivalent to :hg:`COMMAND`
101 # (mainly for :hg:`help -c config`)
102 refuri = make_cmd_ref_uri(args[1])
103 else:
104 refuri = make_any_ref_uri(args[0])
105 node = nodes.reference(rawtext, '', linktext, refuri=refuri)
106 return [node], []
107
108
109 roles.register_local_role("hg", role_hg)
110
111 if __name__ == "__main__":
112 if len(sys.argv) < 2:
113 sys.stderr.write(__doc__ % sys.argv[0])
114 sys.exit(1)
115
116 writer = sys.argv[1]
117 del sys.argv[1]
118
119 if sys.argv[1] == '--hg-individual-pages':
120 is_individual_pages_mode = True
121 del sys.argv[1]
122
123 core.publish_cmdline(writer_name=writer)