1 Star 0 Fork 0

lovelacelee / cppreference-doc

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
build_link_map.py 2.15 KB
一键复制 编辑 原始数据 按行查看 历史
Povilas Kanapickas 提交于 2018-10-24 22:19 . Fix flake8 and pylint warnings
#!/usr/bin/env python3
# Copyright (C) 2012 Povilas Kanapickas <povilas@radix.lt>
#
# This file is part of cppreference-doc
#
# 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 http://www.gnu.org/licenses/.
# This file examines all html files in the output directory and writes
# filename -> title mapping to a xml file.
import fnmatch
import re
import os
from link_map import LinkMap
# returns a dict { title -> filename }.
# directory - either 'output/reference' or 'reference'
def build_link_map(directory):
# find all html files
html_files = []
for root, dirnames, filenames in os.walk(directory):
for filename in fnmatch.filter(filenames, '*.html'):
html_files.append(os.path.join(root, filename))
link_map = LinkMap()
for fn in sorted(html_files):
f = open(fn, "r", encoding='utf-8')
text = f.read()
f.close()
m = re.search(r'<script>[^<]*mw\.config\.set([^<]*wgPageName[^<]*)</script>', text) # noqa
if not m:
continue
text = m.group(1)
text = re.sub(r'\s*', '', text)
m = re.search('"wgPageName":"([^"]*)"', text)
if not m:
continue
title = m.group(1)
target = os.path.relpath(os.path.abspath(fn),
os.path.abspath(directory))
link_map.add_link(title, target)
return link_map
def main():
link_map = build_link_map('output/reference')
# create an xml file containing mapping between page title and actual
# location
link_map.write('output/link-map.xml')
if __name__ == "__main__":
main()
1
https://gitee.com/lovelacelee/cppreference-doc.git
git@gitee.com:lovelacelee/cppreference-doc.git
lovelacelee
cppreference-doc
cppreference-doc
master

搜索帮助