]>
git.proxmox.com Git - ceph.git/blob - ceph/src/seastar/doc/htmlsplit.py
3 # This script takes the single-page HTML output from pandoc - tutorial.html -
4 # and splits it into many pages in split/: one page index.html for the table
5 # of contents, and an additional page for each chapter. We make sure that
6 # links from the TOC to each chapter, and also links across chapters,
7 # continue to work correctly, and also had links from each chapter back to
8 # the TOC, as well as to the next and previous chapters.
11 # Copyright (C) 2018 ScyllaDB.
13 # This file is open source software, licensed to you under the terms
14 # of the Apache License, Version 2.0 (the "License"). See the NOTICE file
15 # distributed with this work for additional information regarding copyright
16 # ownership. You may not use this file except in compliance with the License.
18 # You may obtain a copy of the License at
20 # http://www.apache.org/licenses/LICENSE-2.0
22 # Unless required by applicable law or agreed to in writing,
23 # software distributed under the License is distributed on an
24 # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
25 # KIND, either express or implied. See the License for the
26 # specific language governing permissions and limitations
32 def links(out
, chapter
):
35 out
.write('<A HREF="index.html">Back to table of contents</A>. ')
37 out
.write('Previous: <A HREF="' + str(chapter
-1) +'.html">' + str(chapter
-1) + '. ' + titles
[chapter
-1] + '</A>. ')
41 out
.write('Next: <A HREF="' + str(chapter
+1) +'.html">' + str(chapter
+1) + '. ' + titles
[chapter
+1] + '</A>. ')
44 def flush(chapter
, header
, chunk
):
45 fn
= 'index.html' if chapter
== 0 else str(chapter
) + '.html'
46 with
open('split/' + fn
, 'w') as out
:
51 out
.write('</body></html>')
52 with
open("tutorial.html") as f
:
54 # Chapter currently being read. Set to 0 while reading the TOC, or
55 # numbers > 0 while reading a chapter
58 if line
== '<div id="TOC">\n' or line
=='<nav id="TOC">\n':
62 elif line
.startswith('<h1 id="'):
63 flush(chapter
, header
, chunk
)
66 elif chapter
== 0 and line
.startswith('<li><a href="#'):
67 # For all sections, remember the mapping from name-with-dashes
68 # to the chapter number they are in in "sections". We need this
69 # to support links to other sections.
70 match
= re
.search('href="#([^"]*)".*>([0-9]+)[.<]', line
)
72 sections
[match
.group(1)] = match
.group(2)
73 # replace the link to '#section' with number N.M to chapterN#section
74 match
= re
.match('^(.*href=")(#.*>)([0-9]+)([.<].*)$', line
)
75 line
= match
.group(1) + match
.group(3) + '.html' + match
.group(2) + match
.group(3) + match
.group(4) + '\n'
76 # For chapters, remember the mapping from number to name in the
77 # map "titles", so we can use them later in links to next and
79 match
= re
.search('>([0-9]+)</span> (.*)</a>', line
)
81 titles
[int(match
.group(1))] = match
.group(2)
83 # In a chapter we can have a link to a different subsection, which
84 # looks like <a href="#some-title">Some title</A>. We need to
85 # replace this to refer to the right file after the split.
86 line
= re
.sub('<a href="#([^"]*)">([^<]*)</a>', lambda m
: '<a href="' + sections
[m
.group(1)] + '.html#' + m
.group(1) + '">' + m
.group(2) + '</a>', line
)
88 flush(chapter
, header
, chunk
)