From a1c29603e7d6965fa57e54b340abaccb4a78ab9d Mon Sep 17 00:00:00 2001 From: Oscar Levin Date: Fri, 30 Jul 2021 15:26:46 -0600 Subject: [PATCH 1/3] fix youtube previews and set asymptote conversion to use server --- pretext/build.py | 4 ++-- pretext/static/CORE_COMMIT | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/pretext/build.py b/pretext/build.py index 885f2764..c4078d5f 100644 --- a/pretext/build.py +++ b/pretext/build.py @@ -66,7 +66,7 @@ def diagrams(ptxfile, pub_file, output, params, formats): with utils.working_directory("."): core.asymptote_conversion( xml_source=ptxfile, pub_file=linux_path(pub_file), stringparams=params, - xmlid_root=None, dest_dir=image_output, outformat=formats) + xmlid_root=None, dest_dir=image_output, outformat=formats, method='server') if len(source_xml.xpath("/pretext/*[not(docinfo)]//interactive[not(@preview)]"))> 0: image_output = os.path.abspath( os.path.join(output, 'preview')) @@ -84,7 +84,7 @@ def diagrams(ptxfile, pub_file, output, params, formats): with utils.working_directory("."): core.youtube_thumbnail( xml_source=ptxfile, pub_file=linux_path(pub_file), stringparams=params, - xmlid_root=None, dest_dir=image_output, outformat=formats) + xmlid_root=None, dest_dir=image_output) def webwork(ptxfile, pub_file, dest_dir, params, server_params): diff --git a/pretext/static/CORE_COMMIT b/pretext/static/CORE_COMMIT index 8a959faf..0b47a31c 100644 --- a/pretext/static/CORE_COMMIT +++ b/pretext/static/CORE_COMMIT @@ -1 +1 @@ -1bb1948f5bdb6735f97fa7d418138febc175d0be +82af14faacf09653c5b96ac392860385fb3cf025 From 00fe639f22fe825bc0b940948b74930fe46dd6c2 Mon Sep 17 00:00:00 2001 From: Oscar Levin Date: Fri, 30 Jul 2021 20:17:37 -0600 Subject: [PATCH 2/3] build only default image formats, unless -df all --- pretext/build.py | 26 +++++++++++++++++++------- pretext/cli.py | 4 ++-- pretext/project.py | 5 ++--- 3 files changed, 23 insertions(+), 12 deletions(-) diff --git a/pretext/build.py b/pretext/build.py index c4078d5f..a13c0886 100644 --- a/pretext/build.py +++ b/pretext/build.py @@ -1,3 +1,4 @@ +from re import T from lxml import etree as ET import logging import os @@ -35,13 +36,24 @@ def pdf(ptxfile,pub_file,output,stringparams): None, dest_dir=output) # Function to build diagrams/images contained in source. -def diagrams(ptxfile, pub_file, output, params, formats): +def diagrams(ptxfile, pub_file, output, params, target_format, diagrams_format): + # Dictionary of formats for images based on source and target + formats = { + 'pdf': {'latex-image' : None, 'sageplot': 'pdf', 'asymptote': 'pdf'}, + 'latex': {'latex-image': None, 'sageplot': 'pdf', 'asymptote': 'pdf'}, + 'html': {'latex-image' : 'svg', 'sageplot': 'svg', 'asymptote': 'html'} + } + # set format to all when appropriate + if diagrams_format == 'all': + formats[target_format] = {key: 'all' for key in formats[target_format]} + print(formats) # We assume passed paths are absolute. # set images directory # parse source so we can check for image types. source_xml = ET.parse(ptxfile) source_xml.xinclude() - if len(source_xml.xpath("/pretext/*[not(docinfo)]//latex-image")) > 0: + + if len(source_xml.xpath("/pretext/*[not(docinfo)]//latex-image")) > 0 and formats[target_format]['latex-image'] is not None: image_output = os.path.abspath(os.path.join(output, 'latex-image')) utils.ensure_directory(image_output) log.info('Now generating latex-images\n\n') @@ -49,16 +61,16 @@ def diagrams(ptxfile, pub_file, output, params, formats): with utils.working_directory("."): core.latex_image_conversion( xml_source=ptxfile, pub_file=linux_path(pub_file), stringparams=params, - xmlid_root=None, data_dir=None, dest_dir=image_output, outformat=formats) - if len(source_xml.xpath("/pretext/*[not(docinfo)]//sageplot")) > 0: + xmlid_root=None, dest_dir=image_output, outformat=formats[target_format]['latex-image']) + if len(source_xml.xpath("/pretext/*[not(docinfo)]//sageplot")) > 0 and formats[target_format]['sageplot'] is not None: image_output = os.path.abspath(os.path.join(output, 'sageplot')) utils.ensure_directory(image_output) log.info('Now generating sageplot images\n\n') with utils.working_directory("."): core.sage_conversion( xml_source=ptxfile, pub_file=linux_path(pub_file), stringparams=params, - xmlid_root=None, dest_dir=image_output, outformat=formats) - if len(source_xml.xpath("/pretext/*[not(docinfo)]//asymptote")) > 0: + xmlid_root=None, dest_dir=image_output, outformat=formats[target_format]['sageplot']) + if len(source_xml.xpath("/pretext/*[not(docinfo)]//asymptote")) > 0 and formats[target_format]['asymptote']: image_output = os.path.abspath( os.path.join(output, 'asymptote')) utils.ensure_directory(image_output) @@ -66,7 +78,7 @@ def diagrams(ptxfile, pub_file, output, params, formats): with utils.working_directory("."): core.asymptote_conversion( xml_source=ptxfile, pub_file=linux_path(pub_file), stringparams=params, - xmlid_root=None, dest_dir=image_output, outformat=formats, method='server') + xmlid_root=None, dest_dir=image_output, outformat=formats[target_format]['asymptote'], method='server') if len(source_xml.xpath("/pretext/*[not(docinfo)]//interactive[not(@preview)]"))> 0: image_output = os.path.abspath( os.path.join(output, 'preview')) diff --git a/pretext/cli.py b/pretext/cli.py index 82f2abba..ef418682 100644 --- a/pretext/cli.py +++ b/pretext/cli.py @@ -134,11 +134,11 @@ def init(): Usage: pretext build --stringparam foo bar --stringparam baz woo """) @click.option('-d', '--diagrams', is_flag=True, help='Regenerate images coded in source (latex-image, etc).') -@click.option('-df', '--diagrams-format', default='svg', type=click.Choice(['svg', 'png', 'pdf', 'eps', 'tex', 'all'], case_sensitive=False), help="Specify output format for generated images") +@click.option('-df', '--diagrams-format', type=click.Choice(['defaults', 'all'], case_sensitive=False), default='defaults', help='Specify whether to build just the "defaults" formats or "all" formats based on output target.') @click.option('-w', '--webwork', is_flag=True, default=False, help='Reprocess WeBWorK exercises, creating fresh webwork-representations.ptx file') @click.option('-a', '--only-assets', is_flag=True, default=False, help="Produce requested diagrams (-d) or webwork (-w) but not main build target (useful for large projects that only need to update assets)") @click.option('--clean', is_flag=True, help="Destroy output's target directory before build to clean up previously built files") -def build(target, format, source, output, stringparam, publication, webwork, diagrams, diagrams_format, only_assets,clean): +def build(target, format, source, output, stringparam, publication, webwork, diagrams, diagrams_format, only_assets, clean): """ Process [TARGET] into format specified by project.ptx. Also accepts manual command-line options. diff --git a/pretext/project.py b/pretext/project.py index 32d992bd..51e97eab 100644 --- a/pretext/project.py +++ b/pretext/project.py @@ -186,7 +186,7 @@ def view(self,target_name,access,port,watch=False,build=False): watch_callback=lambda:self.build(target_name) utils.run_server(directory,access,port,watch_directory,watch_callback) - def build(self,target_name,webwork=False,diagrams=False,diagrams_format="svg",only_assets=False,clean=False): + def build(self,target_name,webwork=False,diagrams=False,diagrams_format='defaults',only_assets=False,clean=False): # prepre core PreTeXt pythons scripts self.init_ptxcore() # Check for xml syntax errors and quit if xml invalid: @@ -239,13 +239,12 @@ def build(self,target_name,webwork=False,diagrams=False,diagrams_format="svg",on log.warning( "The source has WeBWorK exercises, but you are not re(processing) these. Run `pretext build` with the `-w` flag if updates are needed.") if diagrams: - builder.diagrams(target.source(), target.publication(), target.generated_dir(), target.stringparams(), diagrams_format) + builder.diagrams(target.source(), target.publication(), target.generated_dir(), target.stringparams(), target.format(), diagrams_format) else: source_xml = target.source_xml() if target.format()=="html" and len(source_xml.xpath('//asymptote|//latex-image|//sageplot')) > 0: log.warning("The source has generated images (, , or ), "+ "but these will not be (re)built. Run `pretext build` with the `-d` flag if updates are needed.") - # TODO: remove the elements that are not needed for latex. if target.format()=="latex" and len(source_xml.xpath('//asymptote|//sageplot|//video[@youtube]|//interactive[not(@preview)]')) > 0: log.warning("The source has interactive elements or videos that need a preview to be generated, "+ "but these will not be (re)built. Run `pretext build` with the `-d` flag if updates are needed.") From 7edcbe79c2a7cc0814120a80543c325636057d37 Mon Sep 17 00:00:00 2001 From: Oscar Levin Date: Fri, 30 Jul 2021 20:26:15 -0600 Subject: [PATCH 3/3] Update pretext/build.py Thanks. --- pretext/build.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/pretext/build.py b/pretext/build.py index a13c0886..b17c9ba0 100644 --- a/pretext/build.py +++ b/pretext/build.py @@ -46,7 +46,6 @@ def diagrams(ptxfile, pub_file, output, params, target_format, diagrams_format): # set format to all when appropriate if diagrams_format == 'all': formats[target_format] = {key: 'all' for key in formats[target_format]} - print(formats) # We assume passed paths are absolute. # set images directory # parse source so we can check for image types. @@ -112,4 +111,3 @@ def webwork(ptxfile, pub_file, dest_dir, params, server_params): core.webwork_to_xml( xml_source=ptxfile, pub_file=None, stringparams=params, abort_early=True, server_params=server_params, dest_dir=dest_dir) -