diff options
author | Sergio Carlavilla Delgado <carlavilla@FreeBSD.org> | 2021-03-31 21:52:38 +0000 |
---|---|---|
committer | Sergio Carlavilla Delgado <carlavilla@FreeBSD.org> | 2021-03-31 21:52:38 +0000 |
commit | cd5051bbedb0a91ba0235e325a15904111f8f670 (patch) | |
tree | 9f98c229b5d052cc128bd483d88ba7d7b4bf9d05 /documentation/tools | |
parent | d7795d4ca0a29c9090c342d41ecf812fe5c9f1ed (diff) | |
download | doc-cd5051bbedb0a91ba0235e325a15904111f8f670.tar.gz doc-cd5051bbedb0a91ba0235e325a15904111f8f670.zip |
Diffstat (limited to 'documentation/tools')
-rw-r--r-- | documentation/tools/books-toc-parts-creator.py | 92 |
1 files changed, 49 insertions, 43 deletions
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 @@ -13,14 +13,14 @@ 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" @@ -30,7 +30,7 @@ def appendCommendAndTitle(language, tocCounter, tocContent): 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): @@ -116,66 +116,72 @@ def main(argv): try: opts, args = getopt.getopt(argv,"hl:",["language="]) except getopt.GetoptError: - print('handbook-toc-creator.py -l <language>') + print('books-toc-creator.py -l <language>') sys.exit(2) for opt, arg in opts: if opt == '-h': - print('handbook-toc-creator.py -l <language>') + print('books-toc-creator.py -l <language>') 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:]) |