diff options
Diffstat (limited to 'documentation/tools/books-toc-creator.py')
| -rw-r--r-- | documentation/tools/books-toc-creator.py | 94 |
1 files changed, 48 insertions, 46 deletions
diff --git a/documentation/tools/books-toc-creator.py b/documentation/tools/books-toc-creator.py index f5b4721a69..340deec339 100644 --- a/documentation/tools/books-toc-creator.py +++ b/documentation/tools/books-toc-creator.py @@ -1,10 +1,8 @@ # -*- coding: utf-8 -*- """ BSD 2-Clause License - Copyright (c) 2020-2021, The FreeBSD Project Copyright (c) 2020-2021, Sergio Carlavilla <carlavilla@FreeBSD.org> - This script will generate the Table of Contents of the Handbook """ #!/usr/bin/env python3 @@ -140,65 +138,69 @@ def main(argv): for language in languages: - with open('./content/{0}/books/handbook/chapters-order.adoc'.format(language), 'r', encoding = 'utf-8') as chaptersFile: - chapters = [line.strip() for line in chaptersFile] + with open('./content/{}/books/books.adoc'.format(language), 'r', encoding = 'utf-8') as booksFile: + books = [line.strip() for line in booksFile] + + for book in books: + with open('./content/{0}/books/{1}/chapters-order.adoc'.format(language,book), 'r', encoding = 'utf-8') as chaptersFile: + chapters = [line.strip() for line in chaptersFile] - toc = "// Code @" + "generated by the FreeBSD Documentation toolchain. DO NOT EDIT.\n" - toc += "// Please don't change this file manually but run `make` to update it.\n" - toc += "// For more information, please read the FreeBSD Documentation Project Primer\n\n" - toc += "[.toc]\n" - toc += "--\n" - toc += '[.toc-title]\n' - toc += setTOCTitle(language) + '\n\n' + toc = "// Code generated by the FreeBSD Documentation toolchain. DO NOT EDIT.\n" + toc += "// Please don't change this file manually but run `make` to update it.\n" + toc += "// For more information, please read the FreeBSD Documentation Project Primer\n\n" + toc += "[.toc]\n" + toc += "--\n" + toc += '[.toc-title]\n' + toc += setTOCTitle(language) + '\n\n' - chapterCounter = 1 - subChapterCounter = 1 - partCounter = 1 - for chapter in chapters: - with open('./content/{0}/books/handbook/{1}'.format(language, chapter), 'r', encoding = 'utf-8') as chapterFile: - chapterContent = chapterFile.read().splitlines() - chapterFile.close() - chapter = chapter.replace("/_index.adoc", "").replace(".adoc", "") + chapterCounter = 1 + subChapterCounter = 1 + partCounter = 1 + for chapter in chapters: + with open('./content/{0}/books/{1}/{2}'.format(language, book, chapter), 'r', encoding = 'utf-8') as chapterFile: + chapterContent = chapterFile.read().splitlines() + chapterFile.close() + chapter = chapter.replace("/_index.adoc", "").replace(".adoc", "") - if checkIsPart(chapter): - for lineNumber, chapterLine in enumerate(chapterContent, 1): + if checkIsPart(chapter): + for lineNumber, chapterLine in enumerate(chapterContent, 1): - if re.match(r"^={1} [^!<\n]+", chapterLine): - toc += "* link:{0}[{1} {2}. {3}]\n".format(chapter, setPartTitle(language), getPartNumber(partCounter), chapterLine.replace("=", "").strip()) - partCounter += 1 + if re.match(r"^={1} [^!<\n]+", chapterLine): + toc += "* link:{0}[{1} {2}. {3}]\n".format(chapter, setPartTitle(language), getPartNumber(partCounter), chapterLine.replace("=", "").strip()) + partCounter += 1 - elif checkIsPreface(chapterContent): - for lineNumber, chapterLine in enumerate(chapterContent, 1): + elif checkIsPreface(chapterContent): + for lineNumber, chapterLine in enumerate(chapterContent, 1): - if re.match(r"^={1} [^!<\n]+", chapterLine): - toc += "* link:{0}[{1}]\n".format(chapter, chapterLine.replace("=", "").strip()) + if re.match(r"^={1} [^!<\n]+", chapterLine): + toc += "* link:{0}[{1}]\n".format(chapter, chapterLine.replace("=", "").strip()) - elif checkIsAppendix(chapterContent): - for lineNumber, chapterLine in enumerate(chapterContent, 1): + elif checkIsAppendix(chapterContent): + for lineNumber, chapterLine in enumerate(chapterContent, 1): - if re.match(r"^={1} [^!<\n]+", chapterLine): - toc += "** link:{0}[{1} {2}]\n".format(chapter, setAppendixTitle(language), chapterLine.replace("=", "").strip()) + if re.match(r"^={1} [^!<\n]+", chapterLine): + toc += "** link:{0}[{1} {2}]\n".format(chapter, setAppendixTitle(language), chapterLine.replace("=", "").strip()) - elif re.match(r"^={2} [^\n]+", chapterLine): - toc += "*** link:{0}/#{1}[{2}]\n".format(chapter, chapterContent[lineNumber-2].replace("[[", "").replace("]]", ""), chapterLine.replace("==", "").lstrip()) + elif re.match(r"^={2} [^\n]+", chapterLine): + toc += "*** link:{0}/#{1}[{2}]\n".format(chapter, chapterContent[lineNumber-2].replace("[[", "").replace("]]", ""), chapterLine.replace("==", "").lstrip()) - else: # Normal chapter - for lineNumber, chapterLine in enumerate(chapterContent, 1): + else: # Normal chapter + for lineNumber, chapterLine in enumerate(chapterContent, 1): - if re.match(r"^={1} [^!<\n]+", chapterLine): - toc += "** link:{0}[{1} {2}. {3}]\n".format(chapter, setChapterTitle(language), chapterCounter, chapterLine.replace("=", "").strip()) + if re.match(r"^={1} [^!<\n]+", chapterLine): + toc += "** link:{0}[{1} {2}. {3}]\n".format(chapter, setChapterTitle(language), chapterCounter, chapterLine.replace("=", "").strip()) - elif re.match(r"^={2} [^\n]+", chapterLine): - toc += "*** link:{0}/#{1}[{2}.{3}. {4}]\n".format(chapter, chapterContent[lineNumber-2].replace("[[", "").replace("]]", ""), chapterCounter, subChapterCounter, chapterLine.replace("==", "").lstrip()) - subChapterCounter += 1 + elif re.match(r"^={2} [^\n]+", chapterLine): + toc += "*** link:{0}/#{1}[{2}.{3}. {4}]\n".format(chapter, chapterContent[lineNumber-2].replace("[[", "").replace("]]", ""), chapterCounter, subChapterCounter, chapterLine.replace("==", "").lstrip()) + subChapterCounter += 1 - chapterCounter += 1 - subChapterCounter = 1 # Reset subChapterCounter + chapterCounter += 1 + subChapterCounter = 1 # Reset subChapterCounter - toc += "--\n" + toc += "--\n" - with open('./content/{0}/books/handbook/toc.adoc'.format(language), 'w', encoding = 'utf-8') as tocFile: - tocFile.write(toc) + with open('./content/{0}/books/{1}/toc.adoc'.format(language, book), 'w', encoding = 'utf-8') as tocFile: + tocFile.write(toc) if __name__ == "__main__": main(sys.argv[1:]) |
