2
2
Convert Docutils' documentation from reStructuredText to <format>.
3
3
"""
4
4
5
+ import contextlib
5
6
from pathlib import Path
6
- import shutil
7
+ import time
7
8
8
9
import docutils
9
10
from docutils import core
16
17
else :
17
18
Trace .show = lambda message , channel : ... # don't print to console
18
19
20
+ DOC_ROOT = Path ("data/docs" ).resolve ()
19
21
20
- def build_html (doc_root , out_root ):
22
+
23
+ def build_html (doc_root ):
24
+ elapsed = 0
21
25
for file in doc_root .rglob ("*.txt" ):
22
- try :
23
- dest = out_root / file . relative_to ( doc_root ). with_suffix ( ".html" )
24
- dest . parent . mkdir ( parents = True , exist_ok = True )
25
- core .publish_file ( source_path = str ( file ) ,
26
- destination_path = str ( dest ) ,
27
- reader_name = "standalone " ,
28
- parser_name = "restructuredtext " ,
29
- writer_name = "html5" ,
30
- settings_overrides = {
31
- "input_encoding " : "utf-8 " ,
32
- "report_level" : 5 ,
33
- })
34
- except docutils . ApplicationError :
35
- ...
36
-
37
-
38
- def bench_docutils (loops , doc_root , out_root ):
39
- runs = []
26
+ file_contents = file . read_text ( encoding = "utf-8" )
27
+ t0 = time . perf_counter_ns ( )
28
+ with contextlib . suppress ( docutils . ApplicationError ):
29
+ core .publish_string ( source = file_contents ,
30
+ reader_name = "standalone" ,
31
+ parser_name = "restructuredtext " ,
32
+ writer_name = "html5 " ,
33
+ settings_overrides = {
34
+ "input_encoding" : "unicode" ,
35
+ "output_encoding " : "unicode " ,
36
+ "report_level" : 5 ,
37
+ })
38
+ elapsed += time . perf_counter_ns () - t0
39
+ return elapsed
40
+
41
+
42
+ def bench_docutils (loops , doc_root ):
43
+ runs_total_ns = 0
40
44
41
45
for _ in range (loops ):
42
- t0 = pyperf .perf_counter ()
43
- build_html (doc_root , out_root )
44
- runs .append (pyperf .perf_counter () - t0 )
45
-
46
- shutil .rmtree (out_root , ignore_errors = True )
46
+ runs_total_ns += build_html (doc_root )
47
47
48
- return sum ( runs )
48
+ return runs_total_ns / 10 ** 9
49
49
50
50
51
51
if __name__ == "__main__" :
@@ -54,7 +54,4 @@ def bench_docutils(loops, doc_root, out_root):
54
54
runner .metadata ['description' ] = "Render documentation with Docutils"
55
55
args = runner .parse_args ()
56
56
57
- DOC_ROOT = Path ("data/docs" ).resolve ()
58
- OUT_ROOT = Path ("data/out" ).resolve ()
59
-
60
- runner .bench_time_func ("docutils" , bench_docutils , DOC_ROOT , OUT_ROOT )
57
+ runner .bench_time_func ("docutils" , bench_docutils , DOC_ROOT )
0 commit comments