X Tutup
Skip to content

Commit ceaf61b

Browse files
authored
Merge pull request p12tic#58 from mokibit/fix-qch-css
Initial work on CSS in qch help
2 parents fad3c5a + 743539d commit ceaf61b

24 files changed

+2978
-130
lines changed

Makefile

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,12 @@ DISTFILES= \
3838
commands/ \
3939
gadgets/ \
4040
headers/ \
41+
index_transform/ \
4142
images/ \
4243
index_transform/ \
4344
reference/ \
4445
skins/ \
46+
tests/ \
4547
build_link_map.py \
4648
ddg_parse_html.py \
4749
devhelp2qch.py \
@@ -173,19 +175,21 @@ output/cppreference-doc-en-cpp.devhelp2: \
173175
#build the .qch (QT help) file
174176
output/cppreference-doc-en-cpp.qch: output/qch-help-project-cpp.xml
175177
#qhelpgenerator only works if the project file is in the same directory as the documentation
176-
cp "output/qch-help-project-cpp.xml" "output/reference/qch.xml"
178+
cp "output/qch-help-project-cpp.xml" "output/reference_cssless/qch.xml"
177179

178-
pushd "output/reference" > /dev/null; \
180+
pushd "output/reference_cssless" > /dev/null; \
179181
$(qhelpgenerator) "qch.xml" -o "../cppreference-doc-en-cpp.qch"; \
180182
popd > /dev/null
181183

182-
rm -f "output/reference/qch.xml"
184+
rm -f "output/reference_cssless/qch.xml"
183185

184-
output/qch-help-project-cpp.xml: output/cppreference-doc-en-cpp.devhelp2
186+
output/qch-help-project-cpp.xml: \
187+
output/cppreference-doc-en-cpp.devhelp2 \
188+
output/reference_cssless
185189
#build the file list
186190
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?><files>" > "output/qch-files.xml"
187191

188-
pushd "output/reference" > /dev/null; \
192+
pushd "output/reference_cssless" > /dev/null; \
189193
find . -type f -not -iname "*.ttf" \
190194
-exec echo "<file>"'{}'"</file>" \; | LC_ALL=C sort >> "../qch-files.xml" ; \
191195
popd > /dev/null
@@ -219,6 +223,10 @@ output/reference:
219223
mkdir -p output
220224
./preprocess.py --src reference --dst output/reference
221225

226+
output/reference_cssless: output/reference
227+
cp -a output/reference output/reference_cssless
228+
./preprocess_qch.py --src output/reference --dst output/reference_cssless
229+
222230
# create indexes for the wiki
223231
indexes:
224232
mkdir -p output/indexes

build_link_map.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
# filename -> title mapping to a xml file.
2222

2323
import fnmatch
24-
import lxml.etree as e
2524
import re
2625
import os
2726
from link_map import LinkMap

commands/preprocess_cssless.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Copyright (C) 2018 Monika Kairaityte <monika@kibit.lt>
2+
#
3+
# This file is part of cppreference-doc
4+
#
5+
# This program is free software: you can redistribute it and/or modify
6+
# it under the terms of the GNU General Public License as published by
7+
# the Free Software Foundation, either version 3 of the License, or
8+
# (at your option) any later version.
9+
#
10+
# This program is distributed in the hope that it will be useful,
11+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
# GNU General Public License for more details.
14+
#
15+
# You should have received a copy of the GNU General Public License
16+
# along with this program. If not, see http://www.gnu.org/licenses/.
17+
18+
from premailer import Premailer
19+
import cssutils
20+
from lxml import html
21+
from lxml import etree
22+
from io import StringIO
23+
from lxml.etree import strip_elements
24+
import logging
25+
import os
26+
import warnings
27+
import io
28+
29+
def preprocess_html_merge_css(src_path, dst_path):
30+
log = logging.Logger('ignore')
31+
output = io.StringIO()
32+
handler = logging.StreamHandler(stream=output)
33+
formatter = logging.Formatter('%(levelname)s, %(message)s')
34+
handler.setFormatter(formatter)
35+
log.addHandler(handler)
36+
# cssutils_logging_handler of Premailer.__init__ is insufficient to silence
37+
# warnings to stderr in non-verbose mode
38+
cssutils.log.setLog(log)
39+
40+
with open(src_path, 'r') as a_file:
41+
content = a_file.read()
42+
parser = etree.HTMLParser()
43+
stripped = content.strip()
44+
root = etree.fromstring(stripped, parser)
45+
46+
with warnings.catch_warnings():
47+
warnings.simplefilter("ignore")
48+
premailer = Premailer(root, base_url=src_path,
49+
disable_link_rewrites=True, remove_classes=True)
50+
51+
root = premailer.transform().getroot()
52+
53+
head = os.path.dirname(dst_path)
54+
os.makedirs(head, exist_ok=True)
55+
56+
# completely remove content of style tags and tags
57+
nondata_tags = ['style']
58+
strip_elements(root, *nondata_tags)
59+
60+
with open(dst_path, 'wb') as a_file:
61+
root.getroottree().write(a_file, pretty_print=True, method="html",
62+
encoding='utf-8')
63+
64+
return output.getvalue()

devhelp2qch.py

Lines changed: 1 addition & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -25,91 +25,6 @@
2525
import sys
2626
import argparse
2727

28-
def convert_toc_lines(source_line, in_section):
29-
i = 0
30-
for el_sub in source_line.getchildren():
31-
el_section_1 = etree.XML('<section/>')
32-
el_section_1.set('title', el_sub.get('name'))
33-
el_section_1.set('ref', el_sub.get('link'))
34-
35-
if el_sub.getchildren() != []:
36-
in_section.append(convert_toc_lines(el_sub, el_section_1))
37-
else:
38-
in_section.append(el_section_1)
39-
40-
return in_section
41-
42-
def convert_toc(in_root_t):
43-
el_toc = etree.XML('<toc/>')
44-
el_section = etree.XML('<section/>')
45-
el_section.set('title', in_root_t.get('title'))
46-
el_section.set('ref', in_root_t.get('link'))
47-
el_toc.append(convert_toc_lines(in_root_t[1], el_section))
48-
return el_toc
49-
50-
def convert_keywords(in_root_k):
51-
el_keywords = etree.XML('<keywords/>')
52-
for el_function in in_root_k[2]:
53-
el_keyword = etree.XML('<keyword/>')
54-
el_keyword.set('name', el_function.get('name'))
55-
el_keyword.set('id', el_function.get('name'))
56-
el_keyword.set('ref', el_function.get('link'))
57-
58-
el_keywords.append(el_keyword)
59-
if el_function.get('name').startswith('std::'):
60-
el_keyword = etree.XML('<keyword/>')
61-
el_keyword.set('name', el_function.get('name'))
62-
63-
# Add an additional id for libc++ users
64-
name_without_std = el_function.get('name')[5:]
65-
66-
el_keyword.set('id', 'std::__LIBCPP_ABI_VERSION::' + name_without_std)
67-
el_keyword.set('ref', el_function.get('link'))
68-
69-
el_keywords.append(el_keyword)
70-
71-
el_keyword = etree.XML('<keyword/>')
72-
el_keyword.set('name', el_function.get('name'))
73-
el_keyword.set('id', 'std::__1::' + name_without_std)
74-
el_keyword.set('ref', el_function.get('link'))
75-
76-
el_keywords.append(el_keyword)
77-
return el_keywords
78-
79-
# Adds files list from external library
80-
def add_files_list(files_root_f):
81-
el_files = etree.XML('<files/>')
82-
for file_item in files_root_f:
83-
el_file = etree.XML('<file/>')
84-
el_file.text = file_item.text
85-
el_files.append(el_file)
86-
return el_files
87-
88-
def convert_devhelp_to_qch(in_root, files_root, out_root, virtual_folder):
89-
el = etree.XML('<namespace/>')
90-
el.text = 'cppreference.com.' + in_root.get('name')
91-
out_root.append(el)
92-
93-
el = etree.XML('<virtualFolder/>')
94-
el.text = virtual_folder
95-
out_root.append(el)
96-
97-
el = etree.XML('<customFilter/>')
98-
el.set('name', in_root.get('title'))
99-
el_filter = etree.XML('<filterAttribute/>')
100-
el_filter.text = in_root.get('name')
101-
el.append(el_filter)
102-
out_root.append(el)
103-
104-
el = etree.XML('<filterSection/>')
105-
el_filter = etree.XML('<filterAttribute/>')
106-
el_filter.text = in_root.get('name')
107-
el.append(el_filter)
108-
el.append(convert_toc(in_root))
109-
el.append(convert_keywords(in_root))
110-
el.append(add_files_list(files_root))
111-
out_root.append(el)
112-
11328
def main():
11429
parser = argparse.ArgumentParser(prog='devhelp2qch.py')
11530
parser.add_argument('--src', type=str, help='The path to the XML input file')
@@ -126,12 +41,9 @@ def main():
12641
parser = etree.XMLParser(encoding='UTF-8', recover=True)
12742
in_tree = etree.parse(src_path, parser)
12843
file_tree = etree.parse(file_path, parser)
129-
out_el = etree.XML('<QtHelpProject xmlns:devhelp="http://www.devhelp.net/book" xmlns:str="http://exslt.org/strings" version="1.0"/>')
130-
131-
convert_devhelp_to_qch(in_tree.getroot(), file_tree.getroot(), out_el, v_folder)
13244

13345
out_f = open(dst_path, 'wb')
134-
out_f.write(etree.tostring(out_el, encoding="utf-8", pretty_print=True, xml_declaration=True))
46+
out_f.write(convert_devhelp_to_qch(in_tree.getroot(), file_tree.getroot(), v_folder)
13547
out_f.close()
13648

13749
if __name__ == "__main__":

fix_devhelp-links.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@
4545
target = '404'
4646
el.set('link', target)
4747

48-
out_f = open(out_fn, 'w', encoding='utf-8')
49-
out_f.write(e.tostring(root, encoding='unicode', pretty_print=True))
48+
out_f = open(out_fn, 'wb')
49+
out_f.write(e.tostring(root, encoding='utf-8', pretty_print=True,
50+
xml_declaration=True))
5051
out_f.close()
5152

index2devhelp.py

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from index_transform.devhelp import *
2222
import argparse
2323
from xml_utils import xml_escape
24+
import io
2425

2526
def main():
2627
parser = argparse.ArgumentParser(prog='index2devhelp')
@@ -40,34 +41,11 @@ def main():
4041
help='the path of the destination file')
4142
args = parser.parse_args()
4243

43-
book_base = args.book_base
44-
chapters_fn = args.chapters_path
45-
book_title = args.book_title
46-
book_name = args.book_name
47-
rel_link = args.rel_link
48-
in_fn = args.in_fn
49-
dest_fn = args.dest_fn
44+
with open(args.dest_fn, 'wb') as out_f:
45+
output = transform_devhelp(args.book_title, args.book_name,
46+
args.book_base, args.rel_link,
47+
args.chapters_path, args.in_fn)
48+
out_f.write(output)
5049

51-
out_f = open(dest_fn, 'w', encoding='utf-8')
52-
53-
out_f.write('<?xml version="1.0"?>\n'
54-
+ '<book title="' + xml_escape(book_title)
55-
+ '" xmlns="http://www.devhelp.net/book'
56-
+ '" name="' + xml_escape(book_name)
57-
+ '" base="' + xml_escape(book_base)
58-
+ '" link="' + xml_escape(rel_link)
59-
+ '" version="2" language="c++">\n')
60-
61-
chapters_f = open(chapters_fn, encoding='utf-8')
62-
out_f.write(chapters_f.read() + '\n')
63-
out_f.write('<functions>')
64-
65-
tr = Index2Devhelp(out_f)
66-
tr.transform_file(in_fn)
67-
68-
out_f.write('''
69-
</functions>
70-
</book>
71-
''')
7250
if __name__ == '__main__':
7351
main()

index_transform/devhelp.py

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,15 @@
2020

2121
from index_transform.common import IndexTransform
2222
from xml_utils import xml_escape
23+
from index_transform import *
24+
from lxml import etree
25+
import io
2326

2427
class Index2Devhelp(IndexTransform):
2528

26-
def __init__(self, out_file):
29+
def __init__(self, functions_el):
2730
super().__init__()
28-
self.out_file = out_file
31+
self.functions_el = functions_el
2932

3033
def get_mark(self, el):
3134
if el.tag == 'const': return 'macro'
@@ -42,8 +45,31 @@ def get_mark(self, el):
4245
return ''
4346

4447
def process_item_hook(self, el, full_name, full_link):
45-
self.out_file.write('<keyword type="' + xml_escape(self.get_mark(el))
46-
+ '" name="' + xml_escape(full_name)
47-
+ '" link="' + xml_escape(full_link) + '"/>\n')
48+
keyword_el = etree.SubElement(self.functions_el, 'keyword')
49+
keyword_el.set('type', self.get_mark(el))
50+
keyword_el.set('name', full_name)
51+
keyword_el.set('link', full_link)
52+
4853
IndexTransform.process_item_hook(self, el, full_name, full_link)
4954

55+
def transform_devhelp(book_title, book_name, book_base, rel_link, chapters_fn,
56+
in_fn):
57+
root_el = etree.Element('book')
58+
root_el.set('xmlns', 'http://www.devhelp.net/book')
59+
root_el.set('title', book_title)
60+
root_el.set('name', book_name)
61+
root_el.set('base', book_base)
62+
root_el.set('link', rel_link)
63+
root_el.set('version', '2')
64+
root_el.set('language', 'c++')
65+
66+
chapters_tree = etree.parse(chapters_fn)
67+
root_el.append(chapters_tree.getroot())
68+
69+
functions_el = etree.SubElement(root_el, 'functions')
70+
71+
tr = Index2Devhelp(functions_el)
72+
tr.transform_file(in_fn)
73+
74+
return etree.tostring(root_el, pretty_print=True, xml_declaration=True,
75+
encoding='utf-8')

0 commit comments

Comments
 (0)
X Tutup