#!/usr/bin/env python3
# This script takes the single-page HTML output from pandoc - tutorial.html -
# and splits it into many pages in split/: one page index.html for the table
# of contents, and an additional page for each chapter. We make sure that
# links from the TOC to each chapter, and also links across chapters,
# continue to work correctly, and also had links from each chapter back to
# the TOC, as well as to the next and previous chapters.
# Copyright (C) 2018 ScyllaDB.
#
# This file is open source software, licensed to you under the terms
# of the Apache License, Version 2.0 (the "License"). See the NOTICE file
# distributed with this work for additional information regarding copyright
# ownership. You may not use this file except in compliance with the License.
#
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
import re
titles = {}
sections = {}
def links(out, chapter):
if chapter == 0:
return
out.write('Back to table of contents. ')
try:
out.write('Previous: ' + str(chapter-1) + '. ' + titles[chapter-1] + '. ')
except:
pass
try:
out.write('Next: ' + str(chapter+1) + '. ' + titles[chapter+1] + '. ')
except:
pass
def flush(chapter, header, chunk):
fn = 'index.html' if chapter == 0 else str(chapter) + '.html'
with open('split/' + fn, 'w') as out:
out.write(header)
links(out, chapter)
out.write(chunk)
links(out, chapter)
out.write('