lv_example.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import os
  2. from docutils import nodes
  3. from docutils.parsers.rst import Directive, directives
  4. from docutils.parsers.rst.directives.images import Image
  5. from sphinx.directives.code import LiteralInclude
  6. def excluded_list(argument):
  7. return argument.split(',')
  8. class LvExample(Directive):
  9. required_arguments = 1
  10. option_spec = {
  11. 'excluded_languages': excluded_list,
  12. 'language': directives.unchanged,
  13. 'description': directives.unchanged
  14. }
  15. def get_example_code_path(self, example_path, language):
  16. return os.path.abspath("../examples/" + example_path + "." + language)
  17. def human_language_name(self, language):
  18. if language == 'py':
  19. return 'MicroPython'
  20. elif language == 'c':
  21. return 'C'
  22. else:
  23. return language
  24. def github_path(self, example_path, language):
  25. env = self.state.document.settings.env
  26. return f"https://github.com/lvgl/lvgl/blob/{env.config.repo_commit_hash}/examples/{example_path}.{language}"
  27. def embed_code(self, example_file, example_path, language, buttons={}):
  28. toggle = nodes.container('', literal_block=False, classes=['toggle'])
  29. header = nodes.container('', literal_block=False, classes=['header'])
  30. toggle.append(header)
  31. try:
  32. with open(example_file) as f:
  33. contents = f.read()
  34. except FileNotFoundError:
  35. contents = 'Error encountered while trying to open ' + example_file
  36. literal_list = nodes.literal_block(contents, contents)
  37. literal_list['language'] = language
  38. toggle.append(literal_list)
  39. paragraph_node = nodes.raw(text=f"<p>{self.human_language_name(language)} code &nbsp;</p>", format='html')
  40. for text, url in buttons.items():
  41. paragraph_node.append(nodes.raw(text=f"<a class='lv-example-link-button' onclick=\"event.stopPropagation();\" href='{url}'>{text}</a>", format='html'))
  42. header.append(paragraph_node)
  43. return toggle
  44. def run(self):
  45. example_path = self.arguments[0]
  46. example_name = os.path.split(example_path)[1]
  47. excluded_languages = self.options.get('excluded_languages', [])
  48. node_list = []
  49. env = self.state.document.settings.env
  50. iframe_html = ""
  51. c_path = self.get_example_code_path(example_path, 'c')
  52. py_path = self.get_example_code_path(example_path, 'py')
  53. c_code = self.embed_code(c_path, example_path, 'c', buttons={
  54. '<i class="fa fa-github"></i>&nbsp;GitHub': self.github_path(example_path, 'c')
  55. })
  56. py_code = self.embed_code(py_path, example_path, 'py', buttons={
  57. '<i class="fa fa-github"></i>&nbsp;GitHub': self.github_path(example_path, 'py'),
  58. '<i class="fa fa-play"></i>&nbsp;Simulator': f"https://sim.lvgl.io/v{env.config.version}/micropython/ports/javascript/index.html?script_startup=https://raw.githubusercontent.com/lvgl/lvgl/{env.config.repo_commit_hash}/examples/header.py&script=https://raw.githubusercontent.com/lvgl/lvgl/{env.config.repo_commit_hash}/examples/{example_path}.py"
  59. })
  60. if not 'c' in excluded_languages:
  61. if env.app.tags.has('html'):
  62. iframe_html = f"<div class='lv-example' data-real-src='/{env.config.version}/_static/built_lv_examples/index.html?example={example_name}&w=320&h=240'></div>"
  63. description_html = f"<div class='lv-example-description'>{self.options.get('description', '')}</div>"
  64. layout_node = nodes.raw(text=f"<div class='lv-example-container'>{iframe_html}{description_html}</div>", format='html')
  65. node_list.append(layout_node)
  66. if not 'c' in excluded_languages:
  67. node_list.append(c_code)
  68. if not 'py' in excluded_languages:
  69. node_list.append(py_code)
  70. trailing_node = nodes.raw(text=f"<hr/>", format='html')
  71. node_list.append(trailing_node)
  72. return node_list
  73. def setup(app):
  74. app.add_directive("lv_example", LvExample)
  75. app.add_config_value("repo_commit_hash", "", "env")
  76. return {
  77. 'version': '0.1',
  78. 'parallel_read_safe': True,
  79. 'parallel_write_safe': True,
  80. }