diff --git a/documentation/tools/books-toc-creator.py b/documentation/tools/books-toc-creator.py index 340deec339..bd85b7e923 100644 --- a/documentation/tools/books-toc-creator.py +++ b/documentation/tools/books-toc-creator.py @@ -1,206 +1,218 @@ +#!/usr/bin/env python3 # -*- 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 +This script will generate the Table of Contents of the books. """ -#!/usr/bin/env python3 import sys, getopt import re languages = [] 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 setPartTitle(language): languages = { 'en': 'Part', 'de': 'Teil', 'el': 'Μέρος', 'es': 'Parte', 'fr': 'Partie', 'hu': 'rész', 'it': 'Parte', 'ja': 'パート', 'mn': 'хэсэг', 'nl': 'Deel', 'pl': 'Część', 'pt-br': 'Parte', '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 getPartNumber(number): numbers = { 1: 'I', 2: 'II', 3: 'III', 4: 'IV', 5: 'V' } return numbers.get(number) 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 main(argv): + justPrintOutput = False + langargs = [] try: - opts, args = getopt.getopt(argv,"hl:",["language="]) + opts, args = getopt.gnu_getopt(argv,"hl:o",["language="]) except getopt.GetoptError: - print('books-toc-creator.py -l ') + print('books-toc-creator.py [-o] -l ') sys.exit(2) for opt, arg in opts: if opt == '-h': - print('books-toc-creator.py -l ') + print('books-toc-creator.py [-o] -l ') sys.exit() + elif opt == '-o': + justPrintOutput = True elif opt in ("-l", "--language"): - languages = arg.split(',') + langargs = arg.replace(" ",",").split(',') + + # treat additional arguments as languages, but check if they + # contain ',' + for l in args: + langargs.extend(l.replace(" ",",").split(',')) - for language in languages: + for language in langargs: 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' 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 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): 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): 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()) 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()) 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 toc += "--\n" - with open('./content/{0}/books/{1}/toc.adoc'.format(language, book), 'w', encoding = 'utf-8') as tocFile: - tocFile.write(toc) + if justPrintOutput == False: + with open('./content/{0}/books/{1}/toc.adoc'.format(language, book), 'w', encoding = 'utf-8') as tocFile: + tocFile.write(toc) + else: + print('./content/{0}/books/{1}/toc.adoc'.format(language, book)) if __name__ == "__main__": main(sys.argv[1:]) diff --git a/documentation/tools/books-toc-examples-creator.py b/documentation/tools/books-toc-examples-creator.py index f72ffa945e..423239c30d 100644 --- a/documentation/tools/books-toc-examples-creator.py +++ b/documentation/tools/books-toc-examples-creator.py @@ -1,122 +1,135 @@ +#!/usr/bin/env python3 # -*- 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 +This script will generate the Table of Contents of any [example]s in the +books. """ -#!/usr/bin/env python3 import sys, getopt import re languages = [] """ To determine if a chapter is a chapter we are going to check if it is anything else, an appendix, a part, the preface ... and if it is not any of those, it will be a chapter. It may not be the best option, but it works :) """ def checkIsChapter(chapter, chapterContent): if "part" in chapter: return False elif "[preface]" in chapterContent: return False elif "[appendix]" in chapterContent: return False else: return True def setTOCTitle(language): languages = { 'en': 'List of Examples', 'de': 'Liste der Beispiele', 'el': 'Κατάλογος Παραδειγμάτων', 'es': 'Lista de ejemplos', 'fr': 'Liste des exemples', 'hu': 'A példák listája', 'it': 'Lista delle tabelle', 'ja': '例の一覧', 'mn': 'Жишээний жагсаалт', 'nl': 'Lijst van voorbeelden', 'pl': 'Spis przykładów', 'pt-br': 'Lista de Exemplos', 'ru': 'Список примеров', 'zh-cn': '范例清单', 'zh-tw': '範例目錄' } return languages.get(language) def main(argv): + justPrintOutput = False + langargs = [] try: - opts, args = getopt.getopt(argv,"hl:",["language="]) + opts, args = getopt.gnu_getopt(argv,"hl:o",["language="]) except getopt.GetoptError: - print('books-toc-examples-creator.py -l ') + print('books-toc-examples-creator.py [-o] -l ') sys.exit(2) for opt, arg in opts: if opt == '-h': - print('books-toc-examples-creator.py -l ') + print('books-toc-examples-creator.py [-o] -l ') sys.exit() + if opt == '-o': + justPrintOutput = True elif opt in ("-l", "--language"): - languages = arg.split(',') + langargs = arg.replace(" ",",").split(',') + + # treat additional arguments as languages, but check if they + # contain ',' + for l in args: + langargs.extend(l.replace(" ",",").split(',')) - for language in languages: + for language in langargs: 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' chapterCounter = 1 exampleCounter = 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", "").replace("/chapter.adoc", "") exampleId = "" exampleTitle = "" for lineNumber, chapterLine in enumerate(chapterContent, 1): if re.match(r"^\[example\]+", chapterLine) and re.match(r"^[.]{1}[^\n]+", chapterContent[lineNumber-2]) and re.match(r"^\[\[[^\n]+\]\]", chapterContent[lineNumber-3]): exampleTitle = chapterContent[lineNumber-2] exampleId = chapterContent[lineNumber-3] if book == "handbook": toc += "* {0}.{1} link:{2}#{3}[{4}]\n".format(chapterCounter, exampleCounter, chapter, exampleId.replace("[[", "").replace("]]", ""), exampleTitle[1:]) else: toc += "* {0}.{1} link:{2}#{3}[{4}]\n".format(chapterCounter, exampleCounter, "", exampleId.replace("[[", "").replace("]]", ""), exampleTitle[1:]) exampleCounter += 1 else: exampleId = "" exampleTitle = "" if checkIsChapter(chapter, chapterContent): chapterCounter += 1 exampleCounter = 1 # Reset example counter toc += "--\n" - with open('./content/{0}/books/{1}/toc-examples.adoc'.format(language, book), 'w', encoding = 'utf-8') as tocFile: - tocFile.write(toc) + if justPrintOutput == False: + with open('./content/{0}/books/{1}/toc-examples.adoc'.format(language, book), 'w', encoding = 'utf-8') as tocFile: + tocFile.write(toc) + else: + print('./content/{0}/books/{1}/toc-examples.adoc'.format(language, book)) if __name__ == "__main__": main(sys.argv[1:]) diff --git a/documentation/tools/books-toc-figures-creator.py b/documentation/tools/books-toc-figures-creator.py index 46bea6226b..fbe1d62c65 100644 --- a/documentation/tools/books-toc-figures-creator.py +++ b/documentation/tools/books-toc-figures-creator.py @@ -1,121 +1,134 @@ +#!/usr/bin/env python3 # -*- 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 +This script will generate the Table of Contents for any figures/images +in the books. """ -#!/usr/bin/env python3 import sys, getopt import re languages = [] """ To determine if a chapter is a chapter we are going to check if it is anything else, an appendix, a part, the preface ... and if it is not any of those, it will be a chapter. It may not be the best option, but it works :) """ def checkIsChapter(chapter, chapterContent): if "part" in chapter: return False elif "[preface]" in chapterContent: return False elif "[appendix]" in chapterContent: return False else: return True def setTOCTitle(language): languages = { 'en': 'List of Figures', 'de': 'Abbildungsverzeichnis', 'el': 'Κατάλογος Σχημάτων', 'es': 'Lista de figuras', 'fr': 'Liste des illustrations', 'hu': 'Az ábrák listája', 'it': 'Lista delle figure', 'ja': '図の一覧', 'mn': 'Зургийн жагсаалт', 'nl': 'Lijst van afbeeldingen', 'pl': 'Spis rysunków', 'pt-br': 'Lista de Figuras', 'ru': 'Список иллюстраций', 'zh-cn': '插图清单', 'zh-tw': '附圖目錄' } return languages.get(language) def main(argv): - + + justPrintOutput = False + langargs = [] try: - opts, args = getopt.getopt(argv,"hl:",["language="]) + opts, args = getopt.gnu_getopt(argv,"hl:o",["language="]) except getopt.GetoptError: - print('books-toc-figures-creator.py -l ') + print('books-toc-figures-creator.py [-o] -l ') sys.exit(2) for opt, arg in opts: if opt == '-h': - print('books-toc-figures-creator.py -l ') + print('books-toc-figures-creator.py [-o] -l ') sys.exit() + if opt == '-o': + justPrintOutput = True elif opt in ("-l", "--language"): - languages = arg.split(',') + langargs = arg.replace(" ",",").split(',') + + # treat additional arguments as languages, but check if they + # contain ',' + for l in args: + langargs.extend(l.replace(" ",",").split(',')) - for language in languages: + for language in langargs: 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' chapterCounter = 1 figureCounter = 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", "").replace("/chapter.adoc", "") figureId = "" figureTitle = "" for lineNumber, chapterLine in enumerate(chapterContent, 1): if re.match(r"^image::+", chapterLine) and re.match(r"^[.]{1}[^\n]+", chapterContent[lineNumber-2]) and re.match(r"^\[\[[^\n]+\]\]", chapterContent[lineNumber-3]): figureTitle = chapterContent[lineNumber-2] figureId = chapterContent[lineNumber-3] if book == "handbook": toc += "* {0}.{1} link:{2}#{3}[{4}]\n".format(chapterCounter, figureCounter, chapter, figureId.replace("[[", "").replace("]]", ""), figureTitle[1:]) else: toc += "* {0}.{1} link:{2}#{3}[{4}]\n".format(chapterCounter, figureCounter, "", figureId.replace("[[", "").replace("]]", ""), figureTitle[1:]) figureCounter += 1 else: figureId = "" figureTitle = "" if checkIsChapter(chapter, chapterContent): chapterCounter += 1 figureCounter = 1 # Reset figure counter toc += "--\n" - with open('./content/{0}/books/{1}/toc-figures.adoc'.format(language, book), 'w', encoding = 'utf-8') as tocFile: - tocFile.write(toc) + if justPrintOutput == False: + with open('./content/{0}/books/{1}/toc-figures.adoc'.format(language, book), 'w', encoding = 'utf-8') as tocFile: + tocFile.write(toc) + else: + print('./content/{0}/books/{1}/toc-figures.adoc'.format(language, book)) if __name__ == "__main__": main(sys.argv[1:]) diff --git a/documentation/tools/books-toc-parts-creator.py b/documentation/tools/books-toc-parts-creator.py index b05aeb9dc5..aeb035b8cb 100644 --- a/documentation/tools/books-toc-parts-creator.py +++ b/documentation/tools/books-toc-parts-creator.py @@ -1,187 +1,204 @@ +#!/usr/bin/env python3 # -*- 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 +This script will generate the Table of Contents for any [preface], +[appendix] or other "parts" as defined by asciidoc. """ -#!/usr/bin/env python3 import sys, getopt import re import os.path languages = [] def cleanTocFile(language, tocCounter, book): - path = './content/{0}/books/{1}/toc-{2}.adoc'.format(language, book, tocCounter) + path = './content/{0}/books/{1}/toc-{2}.adoc'.format(language, book, tocCounter) + if justPrintOutput == True: + print(path) + else: if os.path.exists(path): - tocFile = open(path, 'r+', encoding = 'utf-8') - tocFile.truncate(0) + tocFile = open(path, 'r+', encoding = 'utf-8') + tocFile.truncate(0) + +def prependCommentAndTitle(language, tocCounter, tocContent, book): + if justPrintOutput == True: + return True -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/{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): + global justPrintOutput + justPrintOutput = False + langargs=[] try: - opts, args = getopt.getopt(argv,"hl:",["language="]) + opts, args = getopt.gnu_getopt(argv,"hl:o",["language="]) except getopt.GetoptError: - print('books-toc-creator.py -l ') + print('books-toc-creator.py [-o] -l ') sys.exit(2) for opt, arg in opts: if opt == '-h': - print('books-toc-creator.py -l ') + print('books-toc-creator.py [-o] -l ') sys.exit() + elif opt == '-o': + justPrintOutput = True elif opt in ("-l", "--language"): - languages = arg.split(',') + langargs = arg.replace(" ",",").split(',') + + # treat additional arguments as languages, but check if they + # contain ',' + for l in args: + langargs.extend(l.replace(" ",",").split(',')) - for language in languages: + for language in langargs: 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] chapterCounter = 1 subChapterCounter = 1 partCounter = 0 toc = "" 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): if partCounter > 0: cleanTocFile(language, partCounter, book) - appendCommendAndTitle(language, partCounter, toc, book) + prependCommentAndTitle(language, partCounter, toc, book) toc = "" # Clean toc content partCounter += 1 elif checkIsPreface(chapterContent): pass 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()) elif (re.match(r"^={2} [^\n]+", chapterLine)): toc += "** link:../{0}/#{1}[{2}]\n".format(chapter, chapterContent[lineNumber-2].replace("[[", "").replace("]]", ""), chapterLine.replace("==", "").lstrip()) else: 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()) 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 + prependCommentAndTitle(language, partCounter, toc, book) # For the last part if __name__ == "__main__": main(sys.argv[1:]) diff --git a/documentation/tools/books-toc-tables-creator.py b/documentation/tools/books-toc-tables-creator.py index 592330b7a4..67f1733d45 100644 --- a/documentation/tools/books-toc-tables-creator.py +++ b/documentation/tools/books-toc-tables-creator.py @@ -1,121 +1,133 @@ +#!/usr/bin/env python3 # -*- 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 +This script will generate the Table of Contents for tables in the books. """ -#!/usr/bin/env python3 import sys, getopt import re languages = [] """ To determine if a chapter is a chapter we are going to check if it is anything else, an appendix, a part, the preface ... and if it is not any of those, it will be a chapter. It may not be the best option, but it works :) """ def checkIsChapter(chapter, chapterContent): if "part" in chapter: return False elif "[preface]" in chapterContent: return False elif "[appendix]" in chapterContent: return False else: return True def setTOCTitle(language): languages = { 'en': 'List of Tables', 'de': 'Tabellenverzeichnis', 'el': 'Κατάλογος Πινάκων', 'es': 'Lista de tablas', 'fr': 'Liste des tableaux', 'hu': 'A táblázatok listája', 'it': 'Lista delle tabelle', 'ja': '表の一覧', 'mn': 'Хүснэгтийн жагсаалт', 'nl': 'Lijst van tabellen', 'pl': 'Spis tabel', 'pt-br': 'Lista de Tabelas', 'ru': 'Список таблиц', 'zh-cn': '表格清单', 'zh-tw': '附表目錄' } return languages.get(language) def main(argv): + justPrintOutput = False + langargs= [] try: - opts, args = getopt.getopt(argv,"hl:",["language="]) + opts, args = getopt.gnu_getopt(argv,"hl:o",["language="]) except getopt.GetoptError: - print('books-toc-tables-creator.py -l ') + print('books-toc-tables-creator.py [-o] -l ') sys.exit(2) for opt, arg in opts: if opt == '-h': - print('books-toc-tables-creator.py -l ') + print('books-toc-tables-creator.py [-o] -l ') sys.exit() + if opt == '-o': + justPrintOutput = True elif opt in ("-l", "--language"): - languages = arg.split(',') + langargs = arg.replace(" ",",").split(',') + + # treat additional arguments as languages, but check if they + # contain ',' + for l in args: + langargs.extend(l.replace(" ",",").split(',')) - for language in languages: + for language in langargs: 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' chapterCounter = 1 tableCounter = 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", "").replace("/chapter.adoc", "") tableId = "" tableTitle = "" for lineNumber, chapterLine in enumerate(chapterContent, 1): if re.match(r"^\|\=\=\=+", chapterLine) and re.match(r"^[.]{1}[^\n]+", chapterContent[lineNumber-3]) and re.match(r"^\[\[[^\n]+\]\]", chapterContent[lineNumber-4]): tableTitle = chapterContent[lineNumber-3] tableId = chapterContent[lineNumber-4] if book == "handbook": toc += "* {0}.{1} link:{2}#{3}[{4}]\n".format(chapterCounter, tableCounter, chapter, tableId.replace("[[", "").replace("]]", ""), tableTitle[1:]) else: toc += "* {0}.{1} link:{2}#{3}[{4}]\n".format(chapterCounter, tableCounter, "", tableId.replace("[[", "").replace("]]", ""), tableTitle[1:]) tableCounter += 1 else: tableId = "" tableTitle = "" if checkIsChapter(chapter, chapterContent): chapterCounter += 1 tableCounter = 1 # Reset table counter toc += "--\n" - with open('./content/{0}/books/{1}/toc-tables.adoc'.format(language, book), 'w', encoding = 'utf-8') as tocFile: - tocFile.write(toc) + if justPrintOutput == False: + with open('./content/{0}/books/{1}/toc-tables.adoc'.format(language, book), 'w', encoding = 'utf-8') as tocFile: + tocFile.write(toc) + else: + print('./content/{0}/books/{1}/toc-tables.adoc'.format(language, book)) if __name__ == "__main__": main(sys.argv[1:])