summaryrefslogtreecommitdiff
path: root/lib/lvgl/docs/_ext/lv_example.py
diff options
context:
space:
mode:
authorjacqueline <me@jacqueline.id.au>2023-06-01 15:41:47 +1000
committerjacqueline <me@jacqueline.id.au>2023-06-01 15:41:47 +1000
commitdd27c3530432ea0b09f01e604bf577f31d8ef841 (patch)
treebbf86cf81a78f0ff0b07f31f1c390db473f26fd3 /lib/lvgl/docs/_ext/lv_example.py
parent6fd588e970470b15936187980829916d0dbe77bb (diff)
downloadtangara-fw-dd27c3530432ea0b09f01e604bf577f31d8ef841.tar.gz
convert lvgl from submodule to a plain old directory
Diffstat (limited to 'lib/lvgl/docs/_ext/lv_example.py')
m---------lib/lvgl0
-rw-r--r--lib/lvgl/docs/_ext/lv_example.py98
2 files changed, 98 insertions, 0 deletions
diff --git a/lib/lvgl b/lib/lvgl
deleted file mode 160000
-Subproject 0732400e7b564dd0e7dc4a924619d8e19c5b23a
diff --git a/lib/lvgl/docs/_ext/lv_example.py b/lib/lvgl/docs/_ext/lv_example.py
new file mode 100644
index 00000000..1a818e24
--- /dev/null
+++ b/lib/lvgl/docs/_ext/lv_example.py
@@ -0,0 +1,98 @@
+import os
+
+from docutils import nodes
+from docutils.parsers.rst import Directive, directives
+from docutils.parsers.rst.directives.images import Image
+from sphinx.directives.code import LiteralInclude
+
+
+def excluded_list(argument):
+ return argument.split(',')
+
+
+
+
+class LvExample(Directive):
+ required_arguments = 1
+ option_spec = {
+ 'excluded_languages': excluded_list,
+ 'language': directives.unchanged,
+ 'description': directives.unchanged
+ }
+ def get_example_code_path(self, example_path, language):
+ return os.path.abspath("../examples/" + example_path + "." + language)
+ def human_language_name(self, language):
+ if language == 'py':
+ return 'MicroPython'
+ elif language == 'c':
+ return 'C'
+ else:
+ return language
+ def github_path(self, example_path, language):
+ env = self.state.document.settings.env
+ return f"https://github.com/lvgl/lvgl/blob/{env.config.repo_commit_hash}/examples/{example_path}.{language}"
+ def embed_code(self, example_file, example_path, language, buttons={}):
+ toggle = nodes.container('', literal_block=False, classes=['toggle'])
+ header = nodes.container('', literal_block=False, classes=['header'])
+ toggle.append(header)
+ try:
+ with open(example_file) as f:
+ contents = f.read()
+ except FileNotFoundError:
+ contents = 'Error encountered while trying to open ' + example_file
+ literal_list = nodes.literal_block(contents, contents)
+ literal_list['language'] = language
+ toggle.append(literal_list)
+ paragraph_node = nodes.raw(text=f"<p>{self.human_language_name(language)} code &nbsp;</p>", format='html')
+ for text, url in buttons.items():
+ paragraph_node.append(nodes.raw(text=f"<a class='lv-example-link-button' onclick=\"event.stopPropagation();\" href='{url}'>{text}</a>", format='html'))
+ header.append(paragraph_node)
+ return toggle
+ def run(self):
+ example_path = self.arguments[0]
+ example_name = os.path.split(example_path)[1]
+ excluded_languages = self.options.get('excluded_languages', [])
+ node_list = []
+
+ env = self.state.document.settings.env
+
+ iframe_html = ""
+
+ c_path = self.get_example_code_path(example_path, 'c')
+ py_path = self.get_example_code_path(example_path, 'py')
+
+ c_code = self.embed_code(c_path, example_path, 'c', buttons={
+ '<i class="fa fa-github"></i>&nbsp;GitHub': self.github_path(example_path, 'c')
+ })
+ py_code = self.embed_code(py_path, example_path, 'py', buttons={
+ '<i class="fa fa-github"></i>&nbsp;GitHub': self.github_path(example_path, 'py'),
+ '<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"
+ })
+
+ if not 'c' in excluded_languages:
+ if env.app.tags.has('html'):
+ 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>"
+
+ description_html = f"<div class='lv-example-description'>{self.options.get('description', '')}</div>"
+ layout_node = nodes.raw(text=f"<div class='lv-example-container'>{iframe_html}{description_html}</div>", format='html')
+
+ node_list.append(layout_node)
+ if not 'c' in excluded_languages:
+ node_list.append(c_code)
+ if not 'py' in excluded_languages:
+ node_list.append(py_code)
+
+ trailing_node = nodes.raw(text=f"<hr/>", format='html')
+ node_list.append(trailing_node)
+
+ return node_list
+
+def setup(app):
+ app.add_directive("lv_example", LvExample)
+ app.add_config_value("repo_commit_hash", "", "env")
+
+ return {
+ 'version': '0.1',
+ 'parallel_read_safe': True,
+ 'parallel_write_safe': True,
+ }