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 } 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 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) 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) header.append(nodes.raw(text=f"
{self.human_language_name(language)} code view on GitHub
", 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"", format='html') micropython_node = nodes.raw(text=f"Click to try in the MicroPython simulator!", 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) trailing_node = nodes.raw(text=f"