diff --git a/documentation/tools/books-toc-parts-creator.py b/documentation/tools/books-toc-parts-creator.py index 86ed31fbd1..b05aeb9dc5 100644 --- a/documentation/tools/books-toc-parts-creator.py +++ b/documentation/tools/books-toc-parts-creator.py @@ -1,181 +1,187 @@ # -*- coding: utf-8 -*- """ BSD 2-Clause License Copyright (c) 2020-2021, The FreeBSD Project Copyright (c) 2020-2021, Sergio Carlavilla This script will generate the Table of Contents of the Handbook """ #!/usr/bin/env python3 import sys, getopt import re import os.path languages = [] -def cleanTocFile(language, tocCounter): - path = './content/{0}/books/handbook/toc-{1}.adoc'.format(language, tocCounter) +def cleanTocFile(language, tocCounter, book): + path = './content/{0}/books/{1}/toc-{2}.adoc'.format(language, book, tocCounter) if os.path.exists(path): tocFile = open(path, 'r+', encoding = 'utf-8') tocFile.truncate(0) -def appendCommendAndTitle(language, tocCounter, tocContent): - toc = "// Code @" + "generated by the FreeBSD Documentation toolchain. DO NOT EDIT.\n" +def appendCommendAndTitle(language, tocCounter, tocContent, book): + 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 += tocContent toc += "--\n" - tocFile = open('./content/{0}/books/handbook/toc-{1}.adoc'.format(language, tocCounter), 'a+', encoding = 'utf-8') + tocFile = open('./content/{0}/books/{1}/toc-{2}.adoc'.format(language, book, tocCounter), 'a+', encoding = 'utf-8') tocFile.write(toc) def checkIsPart(chapter): if "part" in chapter: return True return False def checkIsPreface(chapterContent): if "[preface]" in chapterContent: return True return False def checkIsAppendix(chapterContent): if "[appendix]" in chapterContent: return True return False def setAppendixTitle(language): languages = { 'en': 'Appendix', 'de': 'Anhang', 'el': 'Παράρτημα', 'es': 'Apéndice', 'fr': 'Annexe', 'hu': 'függelék', 'it': 'Appendice', 'ja': '付録', 'mn': 'Хавсралт', 'nl': 'Bijlage', 'pl': 'Dodatek', 'pt-br': 'Apêndice', 'ru': 'Приложение', 'zh-cn': '附录', 'zh-tw': '附錄' } return languages.get(language) def setChapterTitle(language): languages = { 'en': 'Chapter', 'de': 'Kapitel', 'el': 'Κεφάλαιο', 'es': 'Capítulo', 'fr': 'Chapitre', 'hu': 'Fejezet', 'it': 'Capitolo', 'ja': '章', 'mn': 'Бүлэг', 'nl': 'Hoofdstuk', 'pl': 'Rozdział', 'pt-br': 'Capítulo', 'ru': 'Глава', 'zh-cn': '章', 'zh-tw': '章' } return languages.get(language) def setTOCTitle(language): languages = { 'en': 'Table of Contents', 'de': 'Inhaltsverzeichnis', 'el': 'Πίνακας Περιεχομένων', 'es': 'Tabla de contenidos', 'fr': 'Table des matières', 'hu': 'Tartalom', 'it': 'Indice', 'ja': '目次', 'mn': 'Гарчиг', 'nl': 'Inhoudsopgave', 'pl': 'Spis treści', 'pt-br': 'Índice', 'ru': 'Содержание', 'zh-cn': '目录', 'zh-tw': '內容目錄' } return languages.get(language) def main(argv): try: opts, args = getopt.getopt(argv,"hl:",["language="]) except getopt.GetoptError: - print('handbook-toc-creator.py -l ') + print('books-toc-creator.py -l ') sys.exit(2) for opt, arg in opts: if opt == '-h': - print('handbook-toc-creator.py -l ') + print('books-toc-creator.py -l ') sys.exit() elif opt in ("-l", "--language"): languages = arg.split(',') for language in languages: - with open('./content/{}/books/handbook/chapters-order.adoc'.format(language), 'r', encoding = 'utf-8') as chaptersFile: - chapters = [line.strip() for line in chaptersFile] - chapterCounter = 1 - subChapterCounter = 1 - partCounter = 0 - toc = "" + with open('./content/{}/books/books.adoc'.format(language), 'r', encoding = 'utf-8') as booksFile: + books = [line.strip() for line in booksFile] - 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", "") + 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] - if checkIsPart(chapter): + chapterCounter = 1 + subChapterCounter = 1 + partCounter = 0 + toc = "" - if partCounter > 0: - cleanTocFile(language, partCounter) - appendCommendAndTitle(language, partCounter, toc) - toc = "" # Clean toc content + 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", "") - partCounter += 1 + if checkIsPart(chapter): - elif checkIsPreface(chapterContent): - pass + if partCounter > 0: + cleanTocFile(language, partCounter, book) + appendCommendAndTitle(language, partCounter, toc, book) + toc = "" # Clean toc content - elif checkIsAppendix(chapterContent): - for lineNumber, chapterLine in enumerate(chapterContent, 1): + partCounter += 1 - if (re.match(r"^={1} [^\n]+", chapterLine)): - toc += "* link:../{0}[{1} {2}]\n".format(chapter, setAppendixTitle(language), chapterLine.replace("=", "").strip()) + elif checkIsPreface(chapterContent): + pass - elif (re.match(r"^={2} [^\n]+", chapterLine)): - toc += "** link:../{0}/#{1}[{2}]\n".format(chapter, chapterContent[lineNumber-2].replace("[[", "").replace("]]", ""), chapterLine.replace("==", "").lstrip()) + elif checkIsAppendix(chapterContent): + for lineNumber, chapterLine in enumerate(chapterContent, 1): - else: - 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}. {3}]\n".format(chapter, setChapterTitle(language), chapterCounter, 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}.{3}. {4}]\n".format(chapter, chapterContent[lineNumber-2].replace("[[", "").replace("]]", ""), chapterCounter, subChapterCounter, chapterLine.replace("==", "").lstrip()) - subChapterCounter += 1 + else: + for lineNumber, chapterLine in enumerate(chapterContent, 1): - chapterCounter += 1 - subChapterCounter = 1 # Reset subChapterCounter + if re.match(r"^={1} [^!<\n]+", chapterLine): + toc += "* link:../{0}[{1} {2}. {3}]\n".format(chapter, setChapterTitle(language), chapterCounter, chapterLine.replace("=", "").strip()) - cleanTocFile(language, partCounter) - appendCommendAndTitle(language, partCounter, toc) # For the last part + 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 + + if partCounter > 0: + cleanTocFile(language, partCounter, book) + appendCommendAndTitle(language, partCounter, toc, book) # For the last part if __name__ == "__main__": main(sys.argv[1:])