2021-06-07 16:07:40 -04:00
import os
2021-04-23 17:45:34 -04:00
from docutils import nodes
2021-06-07 16:07:40 -04:00
from docutils . parsers . rst import Directive
2021-04-23 17:45:34 -04:00
from docutils . parsers . rst . directives . images import Image
from sphinx . directives . code import LiteralInclude
2021-06-07 16:07:40 -04:00
2021-04-23 17:45:34 -04:00
class LvExample ( Directive ) :
required_arguments = 3
def run ( self ) :
example_path = self . arguments [ 0 ]
example_name = os . path . split ( example_path ) [ 1 ]
2021-06-07 16:07:40 -04:00
language = self . arguments [ 2 ]
2021-04-23 17:45:34 -04:00
node_list = [ ]
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 ' )
else :
2021-05-19 08:54:05 -04:00
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 ' )
2021-04-23 17:45:34 -04:00
toggle = nodes . container ( ' ' , literal_block = False , classes = [ ' toggle ' ] )
header = nodes . container ( ' ' , literal_block = False , classes = [ ' header ' ] )
toggle . append ( header )
2021-05-04 20:43:12 -04:00
example_file = os . path . abspath ( " ../examples/ " + example_path + " . " + self . arguments [ 2 ] )
2021-04-23 17:45:34 -04:00
2021-05-04 20:43:12 -04:00
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 )
2021-06-07 16:07:40 -04:00
literal_list [ ' language ' ] = language
2021-04-23 17:45:34 -04:00
toggle . append ( literal_list )
2021-06-07 22:52:17 +02:00
header . append ( nodes . raw ( text = f " <p>code <a class= ' fa fa-github ' href= ' https://github.com/lvgl/lvgl/blob/ { env . config . repo_commit_hash } /examples/ { example_path } . { language } ' > view on GitHub</a></p> " , format = ' html ' ) )
2021-04-23 17:45:34 -04:00
if env . app . tags . has ( ' html ' ) :
node_list . append ( paragraph_node )
node_list . append ( toggle )
return node_list
def setup ( app ) :
app . add_directive ( " lv_example " , LvExample )
2021-06-07 16:07:40 -04:00
app . add_config_value ( " repo_commit_hash " , " " , " env " )
2021-04-23 17:45:34 -04:00
return {
' version ' : ' 0.1 ' ,
' parallel_read_safe ' : True ,
' parallel_write_safe ' : True ,
}