PreTeXtBook / pretext

PreTeXt: an authoring and publishing system for scholarly documents
https://pretextbook.org
Other
264 stars 206 forks source link

Automate processing via helper applications #1295

Open rbeezer opened 4 years ago

rbeezer commented 4 years ago
  1. Make an XSL stylesheet that scans a source document "looking" for items needing extra processing, e.g. <webwork> and <latex-image>. Return a Python list or dictionary of indicators.
  2. In python script, react to indicators and run routines to create the necessary extras and put them in a location where they can be used in later processing steps.

This could also be used for a SageTeX-like feature - a sageeval element could hold Sage code that gets replaced by Sage LaTeX output, with replacement happening via the assembly stylesheet.

StevenClontz commented 4 years ago

Perhaps this can be accomplished with

from lxml import etree
book = etree.parse("book.xml")
def process(book):
  for webwork in book.xpath("//webwork"):
    process_webwork(webwork)
def process_webwork(webwork):
  # do the things

In particular, if it cannot be done with pure XSL, I think factoring the entire logic out to Python/lxml might be preferable.

For "SagePTX", something like...

def process_sageptx(sageptx):
  output = run_sage_on(sageptx.xpath("code")[0].text) # not sure of best way to call sage from within Python
  output_element = etree.Element("output")
  output_element.text = output
  sageptx.append(output_element)

On that note -- we should be mindful of users who want to sage -pip install pretext.. that probably is the best pattern for any processing that requires sage, rather than calling sage from something like os.system('sage foo.sage').

rbeezer commented 4 years ago

Thanks, Stephen. At a minimum, the discovery mentioned here would be an excellent task to delegate to lxml since it is pretty simple (no problem being pinned to XSLT 1.0). Great idea.

Some of the processing you describe above relies on some low-level routines (such as unique ids) down in pretext-common.xsl so I'd need to see how extracting all the right bits and putting the results back together would go in a pure lxml routine. Not being negative, just not obvious to me it'll be better.

rbeezer commented 4 years ago

sage -pip install pretext

That's a thing? ;-) I've been away from Sage development for too long....

kcrisman commented 4 years ago

sage -pip install pretext

That's a thing? ;-)

Amazingly, that is a goal ...

I've been away from Sage development for too long....

True, though one could argue it was time well spent :-)

StevenClontz commented 4 years ago

Well, it's possible that part of process_webwork transforms webwork or book with an XSL document (suggesting that book should also be an argument). Presumably not all, otherwise invoking Python wouldn't be necessary in the first place. I'm not familiar with enough of this side of PreTeXt.

Yeah, having full access to PyPI within Sage is handy. (-: