15
15
16
16
import yaml
17
17
from docutils import frontend , nodes
18
- from docutils .core import default_description , publish_cmdline
18
+ from docutils .core import default_description , publish_cmdline , publish_string
19
+ from docutils .frontend import filter_settings_spec
19
20
from docutils .parsers .rst import Parser as RstParser
21
+ from docutils .writers .html5_polyglot import HTMLTranslator , Writer
20
22
21
23
from myst_parser ._compat import Literal , get_args , get_origin
22
24
from myst_parser .config .main import (
@@ -321,6 +323,27 @@ def parse(self, inputstring: str, document: nodes.document) -> None:
321
323
self .finish_parse ()
322
324
323
325
326
+ class SimpleTranslator (HTMLTranslator ):
327
+ def stylesheet_call (self , * args , ** kwargs ):
328
+ return ""
329
+
330
+
331
+ class SimpleWriter (Writer ):
332
+
333
+ settings_spec = filter_settings_spec (
334
+ Writer .settings_spec ,
335
+ "template" ,
336
+ )
337
+
338
+ def apply_template (self ):
339
+ subs = self .interpolation_dict ()
340
+ return "%(body)s\n " % subs
341
+
342
+ def __init__ (self ):
343
+ self .parts = {}
344
+ self .translator_class = SimpleTranslator
345
+
346
+
324
347
def _run_cli (writer_name : str , writer_description : str , argv : Optional [List [str ]]):
325
348
"""Run the command line interface for a particular writer."""
326
349
publish_cmdline (
@@ -343,6 +366,44 @@ def cli_html5(argv: Optional[List[str]] = None):
343
366
_run_cli ("html5" , "HTML5 documents" , argv )
344
367
345
368
369
+ def cli_html5_demo (argv : Optional [List [str ]] = None ):
370
+ """Cmdline entrypoint for converting MyST to simple HTML5 demonstrations.
371
+
372
+ This is a special case of the HTML5 writer,
373
+ that only outputs the body of the document.
374
+ """
375
+ publish_cmdline (
376
+ parser = Parser (),
377
+ writer = SimpleWriter (),
378
+ description = (
379
+ f"Generates body HTML5 from standalone MyST sources.\n { default_description } "
380
+ ),
381
+ settings_overrides = {
382
+ "doctitle_xform" : False ,
383
+ "sectsubtitle_xform" : False ,
384
+ "initial_header_level" : 1 ,
385
+ },
386
+ argv = argv ,
387
+ )
388
+
389
+
390
+ def to_html5_demo (inputstring : str , ** kwargs ) -> str :
391
+ """Convert a MyST string to HTML5."""
392
+ overrides = {
393
+ "doctitle_xform" : False ,
394
+ "sectsubtitle_xform" : False ,
395
+ "initial_header_level" : 1 ,
396
+ "output_encoding" : "unicode" ,
397
+ }
398
+ overrides .update (kwargs )
399
+ return publish_string (
400
+ inputstring ,
401
+ parser = Parser (),
402
+ writer = SimpleWriter (),
403
+ settings_overrides = overrides ,
404
+ )
405
+
406
+
346
407
def cli_latex (argv : Optional [List [str ]] = None ):
347
408
"""Cmdline entrypoint for converting MyST to LaTeX."""
348
409
_run_cli ("latex" , "LaTeX documents" , argv )
0 commit comments