aboutsummaryrefslogtreecommitdiff
path: root/docs/_ext/lv_example.py
diff options
context:
space:
mode:
authorThemba Dube <embeddedthemba@gmail.com>2021-06-11 16:45:03 -0400
committerThemba Dube <embeddedthemba@gmail.com>2021-06-11 16:48:27 -0400
commit6f37c4fc560c13545177e15576c5b3085c8f2c2a (patch)
tree727cf6e30ad00bc5d1f6d63beab4d0cd65009b1f /docs/_ext/lv_example.py
parent1a62f7a619faa93406bc5895ac3338c232de2226 (diff)
downloadlvgl-6f37c4fc560c13545177e15576c5b3085c8f2c2a.tar.gz
lvgl-6f37c4fc560c13545177e15576c5b3085c8f2c2a.zip
docs(examples) add MicroPython examples
Diffstat (limited to 'docs/_ext/lv_example.py')
-rw-r--r--docs/_ext/lv_example.py68
1 files changed, 50 insertions, 18 deletions
diff --git a/docs/_ext/lv_example.py b/docs/_ext/lv_example.py
index d50851850..34d13ed5c 100644
--- a/docs/_ext/lv_example.py
+++ b/docs/_ext/lv_example.py
@@ -1,30 +1,37 @@
import os
from docutils import nodes
-from docutils.parsers.rst import Directive
+from docutils.parsers.rst import Directive, directives
from docutils.parsers.rst.directives.images import Image
from sphinx.directives.code import LiteralInclude
-class LvExample(Directive):
- required_arguments = 3
- def run(self):
- example_path = self.arguments[0]
- example_name = os.path.split(example_path)[1]
- language = self.arguments[2]
- node_list = []
+def excluded_list(argument):
+ return argument.split(',')
- env = self.state.document.settings.env
- if self.arguments[2] == 'py':
- paragraph_node = nodes.raw(text=f"Click to try in the simulator!<br/><a target='_blank' href='https://sim.lvgl.io/v7/micropython/ports/javascript/bundle_out/index.html?script_startup=https://raw.githubusercontent.com/lvgl/lv_examples/{env.config.example_commit_hash}/src/header.py&script=https://raw.githubusercontent.com/lvgl/lv_examples/{env.config.built_example_commit_hash}/{example_name}/{example_name}.py'><img alt='{example_name}' src='https://raw.githubusercontent.com/lvgl/lv_examples/{env.config.built_example_commit_hash}/{example_name}/{example_name}.png'/></a>", format='html')
+
+
+class LvExample(Directive):
+ required_arguments = 1
+ option_spec = {
+ 'excluded_languages': excluded_list,
+ 'language': 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:
- paragraph_node = nodes.raw(text=f"<iframe class='lv-example' src='/{env.config.version}/_static/built_lv_examples?example={example_name}&w=320&h=240'></iframe>", format='html')
+ return language
+ def embed_code(self, example_file, example_path, language):
+ env = self.state.document.settings.env
toggle = nodes.container('', literal_block=False, classes=['toggle'])
header = nodes.container('', literal_block=False, classes=['header'])
toggle.append(header)
- example_file = os.path.abspath("../examples/" + example_path + "." + self.arguments[2])
-
try:
with open(example_file) as f:
contents = f.read()
@@ -33,10 +40,35 @@ class LvExample(Directive):
literal_list = nodes.literal_block(contents, contents)
literal_list['language'] = language
toggle.append(literal_list)
- header.append(nodes.raw(text=f"<p>code &nbsp; <a class='fa fa-github' href='https://github.com/lvgl/lvgl/blob/{env.config.repo_commit_hash}/examples/{example_path}.{language}'>&nbsp; view on GitHub</a></p>", format='html'))
- if env.app.tags.has('html'):
- node_list.append(paragraph_node)
- node_list.append(toggle)
+ header.append(nodes.raw(text=f"<p>{self.human_language_name(language)} code &nbsp; <a onclick=\"event.stopPropagation();\" class='fa fa-github' href='https://github.com/lvgl/lvgl/blob/{env.config.repo_commit_hash}/examples/{example_path}.{language}'>&nbsp; view on GitHub</a></p>", format='html'))
+ 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_node = nodes.raw(text=f"<iframe class='lv-example' src='/{env.config.version}/_static/built_lv_examples?example={example_name}&w=320&h=240'></iframe>", format='html')
+ micropython_node = nodes.raw(text=f"<a style='display: inline-block; margin-bottom: 1rem;' target='_blank' href='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'>Click to try in the MicroPython simulator!</a>", format='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')
+ py_code = self.embed_code(py_path, example_path, 'py')
+
+ if not 'c' in excluded_languages:
+ if env.app.tags.has('html'):
+ node_list.append(iframe_node)
+ if not 'py' in excluded_languages:
+ node_list.append(micropython_node)
+ if not 'c' in excluded_languages:
+ node_list.append(c_code)
+ if not 'py' in excluded_languages:
+ node_list.append(py_code)
+
return node_list
def setup(app):