From 29756f64081ff303795aea21dfb7bd4258a8d525 Mon Sep 17 00:00:00 2001 From: Timo Koch <timo.koch@iws.uni-stuttgart.de> Date: Wed, 1 Apr 2020 19:06:06 +0200 Subject: [PATCH] [examples] Add uniform fold mechanism for source code to generator --- examples/convert_code_to_doc.py | 11 ++++++++++- examples/generate_example_docs.py | 3 ++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/examples/convert_code_to_doc.py b/examples/convert_code_to_doc.py index b6b97b3024..68a1d847e8 100755 --- a/examples/convert_code_to_doc.py +++ b/examples/convert_code_to_doc.py @@ -50,12 +50,21 @@ def cppRules(): return [suppressHeader, suppressHeaderGuard, suppressEndHeaderGuard, docTransforms] -def transformCode(code, rules): +def transformCode(code, rules, codeFileName): # exclude stuff between [[exclude]] and [[/exclude]] exclude = parseTaggedContent("exclude", action=replaceWith("")) code = exclude.transformString(code) + # Enable toggling content between [[content]] and [[/content]] + def wrapContentIntoDetails(token): + beginDetails = "//\n// <details open>\n" + summmary = "// <summary><b>Click to hide/show the file documentation</b> (or inspect the [source code]({}))</summary>\n//\n".format(codeFileName) + endDetails = "\n//\n// </details>\n" + return beginDetails + summmary + token[0] + endDetails + wrapContent = parseTaggedContent("content", action=wrapContentIntoDetails) + code = wrapContent.transformString(code) + for transform in rules: code = transform.transformString(code) return code diff --git a/examples/generate_example_docs.py b/examples/generate_example_docs.py index d167fe351d..dc97ba7da5 100755 --- a/examples/generate_example_docs.py +++ b/examples/generate_example_docs.py @@ -27,7 +27,8 @@ def convertToMarkdownAndMerge(dir, config): targetFile.write(markdown.read()) elif fileExtension == ".hh" or fileExtension == ".cc": with open(os.path.join(dir, source), "r") as cppCode: - targetFile.write("\n\n" + transformCode(cppCode.read(), cppRules()) + "\n") + sourceRelPath = os.path.relpath(os.path.abspath(os.path.join(dir, source)), os.path.split(os.path.abspath(targetPath))[0]) + targetFile.write("\n\n" + transformCode(cppCode.read(), cppRules(), sourceRelPath) + "\n") else: raise IOError("Unsupported or unknown file extension *{}".format(fileExtension)) -- GitLab