Skip to content
This repository was archived by the owner on Jun 7, 2023. It is now read-only.

Commit eb8b23c

Browse files
committed
Add rs2ptx subcommand
* Convert RST to XML
1 parent 4381c4b commit eb8b23c

File tree

1 file changed

+69
-3
lines changed

1 file changed

+69
-3
lines changed

runestone/__main__.py

Lines changed: 69 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# *********
22
# |docname|
33
# *********
4+
5+
from runestone import cmap
46
import sys
57
import os
68
import shutil
@@ -9,6 +11,7 @@
911
import click
1012
import pathlib
1113
import re
14+
import subprocess
1215
from paver.easy import sh
1316
from pkg_resources import resource_string, resource_filename, require
1417
from .pretext.chapter_pop import manifest_data_to_db
@@ -191,10 +194,14 @@ def build(all, wd):
191194
paver_main(args=myargs)
192195

193196

194-
@cli.command()
197+
@cli.command(short_help="preview the book in a minimal server (NO API support)")
195198
@click.option("--port", default=8000, help="port for server to listen on")
196199
@click.option("--listen", default="", help="address for server to listen on")
197-
def serve(port, listen):
200+
def preview(port, listen):
201+
_preview(port, listen)
202+
203+
204+
def _preview(port, listen):
198205
click.echo("Note: this is a minimal static server without templates or a database.")
199206
click.echo("For many use cases this is fine.")
200207
click.echo(
@@ -246,6 +253,15 @@ def serve(port, listen):
246253
httpd.serve_forever()
247254

248255

256+
# configure preview as an alias for serve
257+
@cli.command(short_help="Deprecated - use preview")
258+
@click.option("--port", default=8000, help="port for server to listen on")
259+
@click.option("--listen", default="", help="address for server to listen on")
260+
def serve(port, listen):
261+
click.echo("The serve command is deprecated, use runestone preview")
262+
_preview(port, listen)
263+
264+
249265
@cli.command()
250266
@click.option("--dest", default="", help="destination for deploy")
251267
def deploy(dest):
@@ -286,7 +302,57 @@ def deploy(dest):
286302
sh("rsync -rav --delete {} {}".format(pavement.serving_dir, dest))
287303

288304

289-
from runestone import cmap
305+
@cli.command(short_help="Run sphinx build to convert RST to PreTeXt")
306+
@click.option("--course", default=None, help="Unique name of the book")
307+
@click.option("--sourcedir", default="_sources", help="Where is the source Luke?")
308+
@click.option("--outdir", default="build", help="Where is the source Luke?")
309+
def rs2ptx(course, sourcedir, outdir):
310+
"""
311+
Assemble and run a sphinx command similar to the following:
312+
313+
.. code-block:: bash
314+
315+
sphinx-build -b xml -d ./build/overview/doctrees -c . -Acourse_id=overview -Alogin_required=false -Aappname=runestone -Aloglevel=10 -Acourse_url=https://runestone.academy -Adynamic_pages=True -Ause_services=true -Abasecourse=overview -Apython3=true -Adownloads_enabled=true -Aallow_pairs=false -Aenable_chatcodes=false -Arunestone_version=5.7.1 -Abuild_info=unknown . ./build/xml
316+
317+
This command demonstrates a step toward independence from paver and the pavement.py file.
318+
This is kind of moot in a future where we rely on pretext to be the authoring language. But it **would**
319+
be easy to move the key variables and template_args from pavement.py to conf.py and update the build
320+
command to work like this...
321+
"""
322+
os.chdir(findProjectRoot())
323+
sys.path.insert(0, os.getcwd())
324+
try:
325+
import pavement
326+
except:
327+
click.echo("Could not read pavement.py file, aborting")
328+
sys.exit(1)
329+
330+
if not course:
331+
if pavement.project_name:
332+
course = pavement.project_name
333+
else:
334+
course = click.prompt("Name of Course ", default="rsbook")
335+
336+
if pavement.template_args:
337+
tdict = pavement.template_args
338+
else:
339+
tdict = {"course_id": course}
340+
341+
cmd_start = ["sphinx-build", "-E", "-b", "xml",
342+
"-d", f"./build/{course}/doctrees", "-c", "."]
343+
tplate_val = [f"-A{key}={val}" for key, val in tdict.items()]
344+
cmd_end = [f"{sourcedir}", f"./{outdir}/xml"]
345+
cmd = " ".join(cmd_start + tplate_val + cmd_end)
346+
click.echo(cmd)
347+
try:
348+
cp = subprocess.run(
349+
cmd , shell=True, check=True
350+
)
351+
except subprocess.CalledProcessError as e:
352+
click.echo(f"{e.stderr or ''}{e.stdout or ''}")
353+
raise
354+
355+
# TODO: Run the xsltproc command after a successful runestone convert
290356

291357

292358
@cli.command(short_help="type runestone doc directive to get help on directive")

0 commit comments

Comments
 (0)