2 # A tool to parse the output of `clang-format --help` and update the
3 # documentation in ../ClangFormat.rst automatically.
10 PARENT_DIR
= os
.path
.join(os
.path
.dirname(__file__
), "..")
11 DOC_FILE
= os
.path
.join(PARENT_DIR
, "ClangFormat.rst")
14 def substitute(text
, tag
, contents
):
15 replacement
= "\n.. START_%s\n\n%s\n\n.. END_%s\n" % (tag
, contents
, tag
)
16 pattern
= r
"\n\.\. START_%s\n.*\n\.\. END_%s\n" % (tag
, tag
)
17 return re
.sub(pattern
, replacement
, text
, flags
=re
.S
)
20 def indent(text
, columns
, indent_first_line
=True):
21 indent_str
= " " * columns
22 s
= re
.sub(r
"\n([^\n])", "\n" + indent_str
+ "\\1", text
, flags
=re
.S
)
23 if not indent_first_line
or s
.startswith("\n"):
28 def get_help_output():
29 args
= ["clang-format", "--help"]
30 cmd
= subprocess
.Popen(args
, stdout
=subprocess
.PIPE
, stderr
=subprocess
.STDOUT
)
31 out
, _
= cmd
.communicate()
32 out
= out
.decode(sys
.stdout
.encoding
)
37 out
= get_help_output()
38 out
= re
.sub(r
" clang-format\.exe ", " clang-format ", out
)
41 """.. code-block:: console
47 out
= indent(out
, 2, indent_first_line
=False)
51 def validate(text
, columns
):
52 for line
in text
.splitlines():
53 if len(line
) > columns
:
54 print("warning: line too long:\n", line
, file=sys
.stderr
)
57 help_text
= get_help_text()
58 validate(help_text
, 100)
60 with
open(DOC_FILE
) as f
:
63 contents
= substitute(contents
, "FORMAT_HELP", help_text
)
65 with
open(DOC_FILE
, "wb") as output
:
66 output
.write(contents
.encode())