File: myrst2man.py

package info (click to toggle)
python-nvchecker 2.16-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 736 kB
  • sloc: python: 4,801; makefile: 25
file content (74 lines) | stat: -rwxr-xr-x 2,076 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/python3

import time
import locale
import os
import sys
try:
  locale.setlocale(locale.LC_ALL, '')
except:
  pass

sys.path.insert(0, '..')
import nvchecker

from docutils.core import publish_cmdline, default_description
from docutils import nodes
from docutils.writers import manpage
from docutils.parsers.rst import roles

def ref_role(
  role, rawtext, text, lineno, inliner,
  options={}, content=[],
):
  node = nodes.reference(rawtext, text.title(), **options)
  return [node], []

def doc_role(
  role, rawtext, text, lineno, inliner,
  options={}, content=[],
):
  node = nodes.reference(rawtext, text, **options)
  return [node], []

roles.register_local_role('ref', ref_role)
roles.register_local_role('doc', doc_role)

class MyTranslator(manpage.Translator):
  def visit_image(self, node):
    raise nodes.SkipNode

  def visit_topic(self, node):
    self.body.append('\n')
    raise nodes.SkipNode

  def visit_title(self, node):
    try:
      super().visit_title(node)
    except nodes.SkipNode:
      if self.section_level == 0:
        self._docinfo['title'] = 'nvchecker'
        self._docinfo['subtitle'] = 'New version checker for software releases'
        self._docinfo['title_upper'] = 'nvchecker'.upper()
        self._docinfo['manual_section'] = '1'
        # Make the generated man page reproducible. Based on the patch from
        # https://sourceforge.net/p/docutils/patches/132/#5333
        source_date_epoch = os.environ.get('SOURCE_DATE_EPOCH')
        if source_date_epoch:
            self._docinfo['date'] = time.strftime('%Y-%m-%d', time.gmtime(int(source_date_epoch)))
        else:
            self._docinfo['date'] = time.strftime('%Y-%m-%d')
        self._docinfo['version'] = nvchecker.__version__
      raise

class MyWriter(manpage.Writer):
  def __init__(self):
    super().__init__()
    self.translator_class = MyTranslator

def main():
  description = ("Generates plain unix manual documents.  " + default_description)
  publish_cmdline(writer=MyWriter(), description=description)

if __name__ == '__main__':
  main()