1import os 2 3from docutils import nodes 4from docutils.parsers.rst import Directive, directives 5# from docutils.parsers.rst.directives.images import Image 6# from sphinx.directives.code import LiteralInclude 7 8 9def excluded_list(argument): 10 return argument.split(',') 11 12 13class LvExample(Directive): 14 required_arguments = 1 15 option_spec = { 16 'excluded_languages': excluded_list, 17 'language': directives.unchanged, 18 'description': directives.unchanged 19 } 20 21 def get_example_code_path(self, example_path, language): 22 base_path = os.path.dirname(__file__) 23 examples_path = os.path.abspath(os.path.join(base_path, '..', 'examples')) 24 example_path = os.path.join(examples_path, example_path + '.' + language) 25 return example_path 26 27 def human_language_name(self, language): 28 if language == 'py': 29 return 'MicroPython' 30 elif language == 'c': 31 return 'C' 32 else: 33 return language 34 35 def github_path(self, example_path, language): 36 env = self.state.document.settings.env 37 return f"https://github.com/lvgl/lvgl/blob/{env.config.repo_commit_hash}/examples/{example_path}.{language}" 38 39 def embed_code(self, example_file, example_path, language, buttons={}): 40 toggle = nodes.container('', literal_block=False, classes=['toggle']) 41 header = nodes.container('', literal_block=False, classes=['header']) 42 toggle.append(header) 43 44 try: 45 with open(example_file, 'rb') as f: 46 contents = f.read().decode('utf-8') 47 except FileNotFoundError: 48 print('File Not Found', example_file) 49 contents = 'Error encountered while trying to open ' + example_file 50 51 literal_list = nodes.literal_block(contents, contents) 52 literal_list['language'] = language 53 toggle.append(literal_list) 54 paragraph_node = nodes.raw(text=f"<p>{self.human_language_name(language)} code </p>", format='html') 55 for text, url in buttons.items(): 56 paragraph_node.append(nodes.raw(text=f"<a class='lv-example-link-button' onclick=\"event.stopPropagation();\" href='{url}'>{text}</a>", format='html')) 57 header.append(paragraph_node) 58 return toggle 59 60 def run(self): 61 example_path = self.arguments[0] 62 example_name = os.path.split(example_path)[1] 63 excluded_languages = self.options.get('excluded_languages', []) 64 node_list = [] 65 66 env = self.state.document.settings.env 67 68 iframe_html = "" 69 70 c_path = self.get_example_code_path(example_path, 'c') 71 py_path = self.get_example_code_path(example_path, 'py') 72 73 if os.path.exists(c_path): 74 c_code = self.embed_code(c_path, example_path, 'c', buttons={ 75 '<i class="fa fa-github"></i> View on GitHub': self.github_path(example_path, 'c') 76 }) 77 else: 78 c_code = None 79 80 if os.path.exists(py_path): 81 py_code = self.embed_code(py_path, example_path, 'py', buttons={ 82 '<i class="fa fa-github"></i> View on GitHub': self.github_path(example_path, 'py'), 83 '<i class="fa fa-play"></i> MicroPython 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" 84 }) 85 else: 86 py_code = None 87 88 if 'c' not in excluded_languages: 89 if env.app.tags.has('html'): 90 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>" 91 92 description_html = f"<div class='lv-example-description'>{self.options.get('description', '')}</div>" 93 layout_node = nodes.raw(text=f"<div class='lv-example-container'>{iframe_html}{description_html}</div>", format='html') 94 95 node_list.append(layout_node) 96 if 'c' not in excluded_languages and c_code is not None: 97 node_list.append(c_code) 98 if 'py' not in excluded_languages and py_code is not None: 99 node_list.append(py_code) 100 101 trailing_node = nodes.raw(text=f"<hr/>", format='html') 102 node_list.append(trailing_node) 103 104 return node_list 105 106 107def setup(app): 108 app.add_directive("lv_example", LvExample) 109 # Direct [View on GitHub] links in examples to use current 110 # branch (stored in LVGL_GITCOMMIT environment variable) instead 111 # of the current commit hash as was being done previously. 112 # Default to 'master' if Sphinx is being run outside of `build.py`. 113 # Resulting example link: 114 # [https://github.com/lvgl/lvgl/blob/master/examples/anim/lv_example_anim_1.c]. 115 # [https://github.com/lvgl/lvgl/blob/v8.4.0/examples/anim/lv_example_anim_1.c]. 116 # [https://github.com/lvgl/lvgl/blob/v9.2.0/examples/anim/lv_example_anim_1.c]. 117 if 'LVGL_GITCOMMIT' in os.environ: 118 git_commit = os.environ['LVGL_GITCOMMIT'] 119 else: 120 git_commit = 'master' 121 122 app.add_config_value("repo_commit_hash", git_commit, "env") 123 124 # if 'repo_commit_hash' in app.config._options: 125 # print(f"repo_commit_hash from lv_example.py: [{app.config._options['repo_commit_hash']}]") 126 # else: 127 # print("repo_commit_hash not found in [app.config._options] at this time.") 128 129 return { 130 'version': '0.1', 131 'parallel_read_safe': True, 132 'parallel_write_safe': True, 133 } 134