Skip to content

Commit

Permalink
Merge pull request #153 from PreTeXtBook/image-defaults
Browse files Browse the repository at this point in the history
Image defaults
  • Loading branch information
StevenClontz authored Jul 31, 2021
2 parents dbfb199 + 7edcbe7 commit b0f3a13
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 14 deletions.
28 changes: 19 additions & 9 deletions pretext/build.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from re import T
from lxml import etree as ET
import logging
import os
Expand Down Expand Up @@ -35,38 +36,48 @@ 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]}
# 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')
# call pretext-core's latex image module:
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)
log.info('Now generating asymptote images\n\n')
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[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'))
Expand All @@ -84,7 +95,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):
Expand All @@ -100,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)

4 changes: 2 additions & 2 deletions pretext/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 2 additions & 3 deletions pretext/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,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:
Expand Down Expand Up @@ -240,13 +240,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 (<latex-image/>, <asymptote/>, or <sageplot/>), "+
"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.")
Expand Down

0 comments on commit b0f3a13

Please sign in to comment.