generate-param-doc-tooltips.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. """
  2. Grab all parameter files (markdown) and convert them to html files
  3. """
  4. import os
  5. import glob
  6. import markdown
  7. parameterDocsFolder = "AI-on-the-edge-device-docs/param-docs/parameter-pages"
  8. docsMainFolder = "../../sd-card/html"
  9. configPage = "edit_config_param.html"
  10. htmlTooltipPrefix = """
  11. <div class="rst-content"><div class="tooltip"><img src="help.png" width="32px"><span class="tooltiptext">
  12. """
  13. htmlTooltipSuffix = """
  14. </span></div></div>
  15. """
  16. folders = sorted( filter( os.path.isdir, glob.glob(parameterDocsFolder + '/*') ) )
  17. def generateHtmlTooltip(section, parameter, markdownFile):
  18. # print(section, parameter, markdownFile)
  19. with open(markdownFile, 'r') as markdownFileHandle:
  20. markdownFileContent = markdownFileHandle.read()
  21. markdownFileContent = markdownFileContent.replace("# ", "### ") # Move all headings 2 level down
  22. htmlTooltip = markdown.markdown(markdownFileContent, extensions=['admonition'])
  23. # Make all links to be opened in a new page
  24. htmlTooltip = htmlTooltip.replace("a href", "a target=_blank href")
  25. # Replace relative documentation links with absolute ones pointing to the external documentation
  26. htmlTooltip = htmlTooltip.replace("href=\"../", "href=\"https://jomjol.github.io/AI-on-the-edge-device-docs/")
  27. # Add custom styles
  28. htmlTooltip = htmlTooltip.replace("<h3>", "<h3 style=\"margin: 0\">")
  29. # Update image paths and copy images to right folder
  30. if "../img/" in htmlTooltip:
  31. htmlTooltip = htmlTooltip.replace("../img/", "/")
  32. htmlTooltip = htmlTooltipPrefix + htmlTooltip + htmlTooltipSuffix
  33. # Add the tooltip to the config page
  34. with open(docsMainFolder + "/" + configPage, 'r') as configPageHandle:
  35. configPageContent = configPageHandle.read()
  36. # print("replacing $TOOLTIP_" + section + "_" + parameter + " with the tooltip content...")
  37. configPageContent = configPageContent.replace("<td>$TOOLTIP_" + section + "_" + parameter + "</td>", "<td>" + htmlTooltip + "</td>")
  38. with open(docsMainFolder + "/" + configPage, 'w') as configPageHandle:
  39. configPageHandle.write(configPageContent)
  40. print("Generating Tooltips...")
  41. """
  42. Generate a HTML tooltip for each markdown page
  43. """
  44. for folder in folders:
  45. folder = folder.split("/")[-1]
  46. files = sorted(filter(os.path.isfile, glob.glob(parameterDocsFolder + "/" + folder + '/*')))
  47. for file in files:
  48. if not ".md" in file: # Skip non-markdown files
  49. continue
  50. parameter = file.split("/")[-1].replace(".md", "")
  51. parameter = parameter.replace("<", "").replace(">", "")
  52. generateHtmlTooltip(folder, parameter, file)
  53. """
  54. Copy images to main folder
  55. """
  56. os.system("cp " + parameterDocsFolder + "/img/* " + docsMainFolder + "/")