summaryrefslogtreecommitdiff
path: root/doc/mkqhcp.py
blob: 2cbee5b9379a33dd98923eb1eba37db578198749 (plain)
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/usr/bin/python3

## mkqhcp.py
## Version 1.0.3

## Copyright 2022-2023 John Donoghue
##
## This program is free software: you can redistribute it and/or modify it
## under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program.  If not, see
## <https://www.gnu.org/licenses/>.

import sys
import os
import re

def process(name):
  with open(name + ".qhcp", 'wt') as f:
    f.write ('<?xml version="1.0" encoding="UTF-8"?>\n')
    f.write ('<QHelpCollectionProject version="1.0">\n')
    f.write ('  <docFiles>\n')
    f.write ('    <generate>\n')
    f.write ('      <file>\n')
    f.write ('        <input>{0}.qhp</input>\n'.format(name))
    f.write ('        <output>{0}.qch</output>\n'.format(name))
    f.write ('      </file>\n')
    f.write ('    </generate>\n')
    f.write ('    <register>\n')
    f.write ('      <file>{0}.qch</file>\n'.format(name))
    f.write ('    </register>\n')
    f.write ('  </docFiles>\n')
    f.write ('</QHelpCollectionProject>\n')

  title = name
  pat_match = re.compile(r".*<title>(?P<title>[^<]+)</title>.*")
  with open(name + ".html", 'rt') as fin:
    # find html
    for line in fin:
      line = line.strip()
      e = pat_match.match(line)
      if e:
          title = e.group("title")
          break

  h2_match = re.compile(r'.*<h2 class="chapter"[^>]*>(?P<title>[^<]+)</h2>.*')
  h3_match = re.compile(r'.*<h3 class="section"[^>]*>(?P<title>[^<]+)</h3>.*')
  h4_match = re.compile(r'.*<h4 class="subsection"[^>]*>(?P<title>[^<]+)</h4>.*')
  tag_match1 = re.compile(r'.*<span id="(?P<tag>[^"]+)"[^>]*></span>.*')
  #tag_match2 = re.compile(r'.*<div class="[sub]*section" id="(?P<tag>[^"]+)"[^>]*>.*')
  tag_match2 = re.compile(r'.*<div class="[sub]*section[^"]*" id="(?P<tag>[^"]+)"[^>]*>.*')
  tag_match3 = re.compile(r'.*<div class="chapter-level-extent" id="(?P<tag>[^"]+)"[^>]*>.*')
  index_match = re.compile(r'.*<h4 class="subsection"[^>]*>[\d\.\s]*(?P<name>[^<]+)</h4>.*')

  tag = "top"
  has_h2 = False 
  has_h3 = False 

  #pat_match = re.compile(r'.*<span id="(?P<tag>[^"])"></span>(?P<title>[.]+)$')
  with open(name + ".html", 'rt') as fin:
    with open(name + ".qhp", 'wt') as f:
      f.write('<?xml version="1.0" encoding="UTF-8"?>\n')
      f.write('<QtHelpProject version="1.0">\n')
      f.write('  <namespace>octave.community.{}</namespace>\n'.format(name))
      f.write('  <virtualFolder>doc</virtualFolder>\n')
      f.write('  <filterSection>\n')
      f.write('    <toc>\n')
      f.write('      <section title="{} Manual" ref="{}.html">\n'.format(title, name))
      # chapters here
      for line in fin:
          line = line.strip()
          e = tag_match1.match(line)
          if not e:
              e = tag_match2.match(line)
          if not e:
              e = tag_match3.match(line)
          if e:
              tag = e.group("tag")

          e = h2_match.match(line)
          if e:
              if has_h3:
                  f.write('          </section>\n')
                  has_h3 = False
              if has_h2:
                  f.write('        </section>\n')
              has_h2 = True
              f.write('        <section title="{}" ref="{}.html#{}">\n'.format(e.group("title"), name, tag))

          e = h3_match.match(line)
          if e:
              if has_h3:
                  f.write('          </section>\n')
              has_h3 = True

              f.write('          <section title="{}" ref="{}.html#{}">\n'.format(e.group("title"), name, tag))

          e = h4_match.match(line)
          if e:
              f.write('            <section title="{}" ref="{}.html#{}"></section>\n'.format(e.group("title"), name, tag))


      if has_h3:
        f.write('          </section>\n')
      if has_h2:
        f.write('        </section>\n')

      f.write('      </section>\n')
      f.write('    </toc>\n')
      f.write('    <keywords>\n')
      
      fin.seek(0)
      for line in fin:
          line = line.strip()
          e = tag_match1.match(line)
          if not e:
              e = tag_match2.match(line)
          if e:
              tag = e.group("tag")

          e = index_match.match(line)
          if e:
              f.write('      <keyword name="{}" ref="{}.html#{}"></keyword>\n'.format(e.group("name"), name, tag))

      f.write('    </keywords>\n')
      f.write('    <files>\n')
      f.write('      <file>{}.html</file>\n'.format(name))
      f.write('      <file>{}.css</file>\n'.format(name))
      f.write('    </files>\n')
      f.write('  </filterSection>\n')
      f.write('</QtHelpProject>\n')


def show_usage():
  print (sys.argv[0], "projname")

if __name__ == "__main__":
  if len(sys.argv) > 1:
    status = process(sys.argv[1])
    sys.exit(status)
  else:
    show_usage()