@@ -92,6 +92,7 @@ def register_docutils(v, ctx):
9292 'stylesheet_path' : None ,
9393 'input_encoding' : 'utf-8' ,
9494 'output_encoding' : 'utf-8' ,
95+ 'exit_status_level' : 3 ,
9596 'alectryon_banner' : ctx ["include_banner" ],
9697 'alectryon_vernums' : ctx ["include_vernums" ],
9798 'alectryon_webpage_style' : ctx ["webpage_style" ],
@@ -102,17 +103,26 @@ def register_docutils(v, ctx):
102103def _gen_docutils (source , fpath ,
103104 Parser , Reader , Writer ,
104105 settings_overrides ):
105- from docutils .core import publish_string
106+ from docutils .core import publish_programmatically
107+ from docutils .io import StringInput , StringOutput
106108
107109 parser = Parser ()
108- return publish_string (
109- source = source .encode ("utf-8" ),
110+ output , pub = publish_programmatically (
111+ source_class = StringInput , destination_class = StringOutput ,
112+ source = source .encode ("utf-8" ), destination = None ,
110113 source_path = fpath , destination_path = None ,
114+
111115 reader = Reader (parser ), reader_name = None ,
112116 parser = parser , parser_name = None ,
113117 writer = Writer (), writer_name = None ,
114- settings_overrides = settings_overrides ,
115- enable_exit_status = True ).decode ("utf-8" )
118+
119+ settings = None , settings_spec = None ,
120+ settings_overrides = settings_overrides , config_section = None ,
121+ enable_exit_status = False )
122+
123+ max_level = pub .document .reporter .max_level
124+ exit_code = max_level + 10 if max_level >= pub .settings .exit_status_level else 0
125+ return output .decode ("utf-8" ), exit_code
116126
117127def _resolve_dialect (backend , html_dialect , latex_dialect ):
118128 return {"webpage" : html_dialect , "latex" : latex_dialect }.get (backend , None )
@@ -122,15 +132,17 @@ def _record_assets(assets, path, names):
122132 assets .append ((path , name ))
123133
124134def gen_docutils (src , frontend , backend , fpath , dialect ,
125- docutils_settings_overrides , assets ):
135+ docutils_settings_overrides , assets , exit_code ):
126136 from .docutils import get_pipeline
127137
128138 pipeline = get_pipeline (frontend , backend , dialect )
129139 _record_assets (assets , pipeline .translator .ASSETS_PATH , pipeline .translator .ASSETS )
130140
131- return _gen_docutils (src , fpath ,
132- pipeline .parser , pipeline .reader , pipeline .writer ,
133- docutils_settings_overrides )
141+ output , exit_code .val = \
142+ _gen_docutils (src , fpath ,
143+ pipeline .parser , pipeline .reader , pipeline .writer ,
144+ docutils_settings_overrides )
145+ return output
134146
135147def _docutils_cmdline (description , frontend , backend ):
136148 import locale
@@ -146,24 +158,7 @@ def _docutils_cmdline(description, frontend, backend):
146158 publish_cmdline (
147159 parser = pipeline .parser (), writer = pipeline .writer (),
148160 settings_overrides = {'stylesheet_path' : None },
149- description = "{} {}" .format (description , default_description )
150- )
151-
152- def lint_docutils (source , fpath , frontend , docutils_settings_overrides ):
153- from docutils .core import publish_doctree
154- from .docutils import get_parser , LintingReader
155-
156- parser = get_parser (frontend )()
157- reader = LintingReader (parser )
158-
159- publish_doctree (
160- source = source .encode ("utf-8" ), source_path = fpath ,
161- reader = reader , reader_name = None ,
162- parser = parser , parser_name = None ,
163- settings_overrides = docutils_settings_overrides ,
164- enable_exit_status = True )
165-
166- return reader .error_stream .getvalue () # FIXME exit code
161+ description = "{} {}" .format (description , default_description ))
167162
168163def _scrub_fname (fname ):
169164 import re
@@ -380,7 +375,7 @@ def write_file(ext):
380375 (read_plain , parse_coq_plain , annotate_chunks , apply_transforms ,
381376 gen_latex_snippets , dump_latex_snippets , write_file (".snippets.tex" )),
382377 'lint' :
383- (read_plain , register_docutils , lint_docutils ,
378+ (read_plain , register_docutils , gen_docutils ,
384379 write_file (".lint.json" )),
385380 'rst' :
386381 (read_plain , coq_to_rst , write_file (".v.rst" )),
@@ -396,7 +391,7 @@ def write_file(ext):
396391 (read_plain , register_docutils , gen_docutils , copy_assets ,
397392 write_file (".tex" )),
398393 'lint' :
399- (read_plain , register_docutils , lint_docutils ,
394+ (read_plain , register_docutils , gen_docutils ,
400395 write_file (".lint.json" )),
401396 'rst' :
402397 (read_plain , coq_to_rst , write_file (".v.rst" ))
@@ -415,7 +410,7 @@ def write_file(ext):
415410 (read_plain , register_docutils , gen_docutils , copy_assets ,
416411 write_file (".tex" )),
417412 'lint' :
418- (read_plain , register_docutils , lint_docutils ,
413+ (read_plain , register_docutils , gen_docutils ,
419414 write_file (".lint.json" )),
420415 'coq' :
421416 (read_plain , rst_to_coq , write_file (".v" )),
@@ -430,7 +425,7 @@ def write_file(ext):
430425 (read_plain , register_docutils , gen_docutils , copy_assets ,
431426 write_file (".tex" )),
432427 'lint' :
433- (read_plain , register_docutils , lint_docutils ,
428+ (read_plain , register_docutils , gen_docutils ,
434429 write_file (".lint.json" ))
435430 }
436431}
@@ -694,6 +689,10 @@ def parse_arguments():
694689# Entry point
695690# ===========
696691
692+ class ExitCode :
693+ def __init__ (self , n ):
694+ self .val = n
695+
697696def call_pipeline_step (step , state , ctx ):
698697 params = list (inspect .signature (step ).parameters .keys ())[1 :]
699698 return step (state , ** {p : ctx [p ] for p in params })
@@ -708,7 +707,7 @@ def build_context(fpath, args, frontend, backend):
708707 ctx = {** vars (args ),
709708 "fpath" : fpath , "fname" : fname ,
710709 "frontend" : frontend , "backend" : backend , "dialect" : dialect ,
711- "assets" : [], "html_classes" : []}
710+ "assets" : [], "html_classes" : [], "exit_code" : ExitCode ( 0 ) }
712711 ctx ["ctx" ] = ctx
713712
714713 if args .output_directory is None :
@@ -742,11 +741,12 @@ def process_pipelines(args):
742741 state , ctx = None , build_context (fpath , args , frontend , backend )
743742 for step in pipeline :
744743 state = call_pipeline_step (step , state , ctx )
744+ yield ctx ["exit_code" ].val
745745
746746def main ():
747747 try :
748748 args = parse_arguments ()
749- process_pipelines (args )
749+ sys . exit ( max ( process_pipelines (args ), default = 0 ) )
750750 except (ValueError , FileNotFoundError , ImportError , argparse .ArgumentTypeError ) as e :
751751 if core .TRACEBACK :
752752 raise e
0 commit comments