<div dir="ltr">Hi Chris and Dr. Joel,<div><br></div><div>Here is the latest version based on various comments, what do think about it? Is there anything else you want to add or change?</div><div><br></div><div>Best,</div><div>Dannie</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Aug 3, 2018 at 10:19 PM, Dannie Huang <span dir="ltr"><<a href="mailto:danxue.huang@gmail.com" target="_blank">danxue.huang@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">From: Danxue Huang <<a href="mailto:36866155%2Bdh0072@users.noreply.github.com">36866155+dh0072@users.<wbr>noreply.github.com</a>><br>
<br>
---<br>
.gitignore | 3 ++<br>
README.md | 2 +<br>
gen_rst_from_makedoc.py | 61 ++++++++++++++++++++++++++++<br>
makedoc2rst.py | 99 ++++++++++++++++++++++++++++++<wbr>+++++++++++++++<br>
rst.py | 104 ++++++++++++++++++++++++++++++<wbr>++++++++++++++++++<br>
5 files changed, 269 insertions(+)<br>
create mode 100644 .gitignore<br>
create mode 100644 README.md<br>
create mode 100755 gen_rst_from_makedoc.py<br>
create mode 100644 makedoc2rst.py<br>
create mode 100644 rst.py<br>
<br>
diff --git a/.gitignore b/.gitignore<br>
new file mode 100644<br>
index 0000000..f707fbd<br>
--- /dev/null<br>
+++ b/.gitignore<br>
@@ -0,0 +1,3 @@<br>
+.idea/*<br>
+__pycache__/*<br>
+*.rst<br>
diff --git a/README.md b/README.md<br>
new file mode 100644<br>
index 0000000..64a3ba9<br>
--- /dev/null<br>
+++ b/README.md<br>
@@ -0,0 +1,2 @@<br>
+# NewlibMarkup2SphinxConverter<br>
+This repo contains code for NewlibMarkup2SphinxConverter<br>
diff --git a/gen_rst_from_makedoc.py b/gen_rst_from_makedoc.py<br>
new file mode 100755<br>
index 0000000..8e4d9b0<br>
--- /dev/null<br>
+++ b/gen_rst_from_makedoc.py<br>
@@ -0,0 +1,61 @@<br>
+#!/usr/bin/env python<br>
+#<br>
+# RTEMS Tools Project (<a href="http://www.rtems.org/" rel="noreferrer" target="_blank">http://www.rtems.org/</a>)<br>
+# Copyright 2018 Danxue Huang (<a href="mailto:danxue.huang@gmail.com">danxue.huang@gmail.com</a>)<br>
+# All rights reserved.<br>
+#<br>
+# This file is part of the RTEMS Tools package in 'rtems-tools'.<br>
+#<br>
+# Redistribution and use in source and binary forms, with or without<br>
+# modification, are permitted provided that the following conditions are met:<br>
+#<br>
+# 1. Redistributions of source code must retain the above copyright notice,<br>
+# this list of conditions and the following disclaimer.<br>
+#<br>
+# 2. Redistributions in binary form must reproduce the above copyright notice,<br>
+# this list of conditions and the following disclaimer in the documentation<br>
+# and/or other materials provided with the distribution.<br>
+#<br>
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"<br>
+# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE<br>
+# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE<br>
+# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE<br>
+# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR<br>
+# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF<br>
+# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS<br>
+# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN<br>
+# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)<br>
+# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE<br>
+# POSSIBILITY OF SUCH DAMAGE.<br>
+#<br>
+<br>
+import argparse<br>
+import makedoc2rst<br>
+<br>
+<br>
+def get_parser():<br>
+ parser = argparse.ArgumentParser(<br>
+ description='Convert newlib style markup to rst markup'<br>
+ )<br>
+ parser.add_argument(<br>
+ '-c',<br>
+ '--c_file_path',<br>
+ type=str,<br>
+ help='Path of c source file with newlib style comments',<br>
+ )<br>
+ parser.add_argument(<br>
+ '-r',<br>
+ '--rst_file_path',<br>
+ type=str,<br>
+ help='Path of destination file with rst markup',<br>
+ )<br>
+ return parser<br>
+<br>
+<br>
+def main(c_file, rst_file):<br>
+ makedoc2rst.makedoc2rst(c_<wbr>file, rst_file).convert()<br>
+<br>
+<br>
+if __name__ == '__main__':<br>
+ args = get_parser().parse_args()<br>
+ main(args.c_file_path, args.rst_file_path)<br>
diff --git a/makedoc2rst.py b/makedoc2rst.py<br>
new file mode 100644<br>
index 0000000..d887323<br>
--- /dev/null<br>
+++ b/makedoc2rst.py<br>
@@ -0,0 +1,99 @@<br>
+#!/usr/bin/env python<br>
+#<br>
+# RTEMS Tools Project (<a href="http://www.rtems.org/" rel="noreferrer" target="_blank">http://www.rtems.org/</a>)<br>
+# Copyright 2018 Danxue Huang (<a href="mailto:danxue.huang@gmail.com">danxue.huang@gmail.com</a>)<br>
+# All rights reserved.<br>
+#<br>
+# This file is part of the RTEMS Tools package in 'rtems-tools'.<br>
+#<br>
+# Redistribution and use in source and binary forms, with or without<br>
+# modification, are permitted provided that the following conditions are met:<br>
+#<br>
+# 1. Redistributions of source code must retain the above copyright notice,<br>
+# this list of conditions and the following disclaimer.<br>
+#<br>
+# 2. Redistributions in binary form must reproduce the above copyright notice,<br>
+# this list of conditions and the following disclaimer in the documentation<br>
+# and/or other materials provided with the distribution.<br>
+#<br>
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"<br>
+# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE<br>
+# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE<br>
+# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE<br>
+# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR<br>
+# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF<br>
+# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS<br>
+# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN<br>
+# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)<br>
+# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE<br>
+# POSSIBILITY OF SUCH DAMAGE.<br>
+#<br>
+<br>
+import re<br>
+import rst<br>
+<br>
+<br>
+class makedoc2rst():<br>
+ """ Convert c source file with makedoc markup to rst markup file<br>
+ c_file: c source file containing block comments (/* */) at the beginning<br>
+ rst_file: destination file with rst markup<br>
+ """<br>
+ def __init__(self, c_file, rst_file):<br>
+ self.c_file = c_file<br>
+ self.rst_file = rst_file<br>
+<br>
+ def convert(self):<br>
+ """ Implementation of converting c file to rst file """<br>
+ rst_str = ''<br>
+ with open(self.c_file, 'r') as c_file:<br>
+ # Get comments inside of /* */<br>
+ comments = self._extract_comments(c_file.<wbr>read())<br>
+ # Parse comments<br>
+ command_text_dict = self._extract_command_and_<wbr>text(comments)<br>
+ # Process text based on command type<br>
+ for command, text in command_text_dict.items():<br>
+ rst_str += rst.get_command_processor(<wbr>command)(command, text)<br>
+<br>
+ with open(self.rst_file, 'w') as rst_file:<br>
+ rst_file.write(rst_str)<br>
+ return rst_str<br>
+<br>
+ def _is_command(self, s):<br>
+ """<br>
+ A command is a single word of at least 3 characters, all uppercase<br>
+ :param s: input string<br>
+ :return: True if s is a single command, otherwise False<br>
+ """<br>
+ return True if re.match('^[A-Z_]{3,}\s*$', s) else False<br>
+<br>
+ def _extract_comments(self, content):<br>
+ """<br>
+ Extract content inside of /* */<br>
+ :param content: input file content<br>
+ :return: extracted comments<br>
+ """<br>
+ comments = ''<br>
+ comments_match = re.match('/\*(\*(?!/)|[^*])*\*<wbr>/', content)<br>
+ if comments_match:<br>
+ wrapped_comments = comments_match.group()<br>
+ comments = wrapped_comments.lstrip('/*').<wbr>rstrip('*/').lstrip().rstrip()<br>
+ return comments<br>
+<br>
+ def _extract_command_and_text(<wbr>self, comments):<br>
+ """<br>
+ Extract command and text from input string content<br>
+ :param comments: input string containing command and text<br>
+ :return: a tuple containing command and text<br>
+ """<br>
+ command = ''<br>
+ text = ''<br>
+ command_text_dict = {}<br>
+ for line in comments.splitlines():<br>
+ if self._is_command(line):<br>
+ if command and text:<br>
+ command_text_dict[command] = text<br>
+ command = line.rstrip()<br>
+ text = ''<br>
+ else:<br>
+ text += line + '\n'<br>
+ return command_text_dict<br>
diff --git a/rst.py b/rst.py<br>
new file mode 100644<br>
index 0000000..2531f46<br>
--- /dev/null<br>
+++ b/rst.py<br>
@@ -0,0 +1,104 @@<br>
+#<br>
+# RTEMS Tools Project (<a href="http://www.rtems.org/" rel="noreferrer" target="_blank">http://www.rtems.org/</a>)<br>
+# Copyright 2018 Danxue Huang (<a href="mailto:danxue.huang@gmail.com">danxue.huang@gmail.com</a>)<br>
+# All rights reserved.<br>
+#<br>
+# This file is part of the RTEMS Tools package in 'rtems-tools'.<br>
+#<br>
+# Redistribution and use in source and binary forms, with or without<br>
+# modification, are permitted provided that the following conditions are met:<br>
+#<br>
+# 1. Redistributions of source code must retain the above copyright notice,<br>
+# this list of conditions and the following disclaimer.<br>
+#<br>
+# 2. Redistributions in binary form must reproduce the above copyright notice,<br>
+# this list of conditions and the following disclaimer in the documentation<br>
+# and/or other materials provided with the distribution.<br>
+#<br>
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"<br>
+# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE<br>
+# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE<br>
+# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE<br>
+# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR<br>
+# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF<br>
+# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS<br>
+# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN<br>
+# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)<br>
+# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE<br>
+# POSSIBILITY OF SUCH DAMAGE.<br>
+#<br>
+<br>
+<br>
+import re<br>
+<br>
+<br>
+def register_processor_command():<br>
+ return {<br>
+ 'FUNCTION': gen_function_summary,<br>
+ 'SYNOPSIS': gen_code_block,<br>
+ 'ANSI_SYNOPSIS': gen_code_block,<br>
+ 'TRAD_SYNOPSIS': gen_code_block,<br>
+ 'TYPEDEF': gen_code_block,<br>
+ 'DESCRIPTION': gen_custom_directives,<br>
+ 'INDEX': gen_custom_directives,<br>
+ 'RETURNS': gen_custom_directives,<br>
+ 'PORTABILITY': gen_custom_directives,<br>
+ 'NOTES': gen_custom_directives,<br>
+ 'ERRORS': gen_custom_directives,<br>
+ 'BUGS': gen_custom_directives,<br>
+ 'WARNINGS': gen_custom_directives,<br>
+ 'QUICKREF': gen_nothing,<br>
+ 'MATHREF': gen_nothing,<br>
+ 'NEWPAGE': gen_nothing,<br>
+ 'START': gen_nothing,<br>
+ 'END': gen_nothing,<br>
+ 'SEEALSO': gen_custom_directives,<br>
+ }<br>
+<br>
+<br>
+def get_command_processor(command)<wbr>:<br>
+ command_processor_dict = register_processor_command()<br>
+ if command in command_processor_dict:<br>
+ return command_processor_dict[<wbr>command]<br>
+ else:<br>
+ print('Command {c} is not recognized, skip it'.format(c=command))<br>
+ return gen_nothing<br>
+<br>
+<br>
+def gen_function_summary(command, text):<br>
+ function_name = extract_function_name(text)<br>
+ summary = extract_summary(text)<br>
+<br>
+ title = '.. {f}:\n\n{f} - {s}\n'.format(<br>
+ f=function_name,<br>
+ s=summary.capitalize()<br>
+ )<br>
+ dashes = '-' * len(text) + '\n'<br>
+ func_name_index = '.. index:: {name}\n'.format(name=<wbr>function_name)<br>
+ summary_index = '.. index:: {summary}\n\n'.format(summary=<wbr>summary)<br>
+ return title + dashes + func_name_index + summary_index<br>
+<br>
+<br>
+def extract_function_name(text):<br>
+ function_name = ''<br>
+ function_name_match = re.match('\s+(<<(>(?!>)|[^>])*<wbr>>>)', text)<br>
+ if function_name_match:<br>
+ function_name = function_name_match.group(1).<wbr>lstrip('<<').rstrip('>>')<br>
+ return function_name<br>
+<br>
+<br>
+def extract_summary(text):<br>
+ return text.split('---')[-1].rstrip()<br>
+<br>
+<br>
+def gen_code_block(command, text):<br>
+ return '**{c}:**\n\n.. code-block:: c\n\n{t}\n\n'.format(c = command, t=text)<br>
+<br>
+<br>
+def gen_nothing(command, text):<br>
+ return '\n\n'<br>
+<br>
+<br>
+def gen_custom_directives(command, text):<br>
+ return '**{c}:**\n\n{t}\n\n'.format(<wbr>c=command, t=text)<br>
+<br>
<span class="HOEnZb"><font color="#888888">-- <br>
2.7.4<br>
<br>
</font></span></blockquote></div><br></div>