4 This script generates markdown files for all the MAVLink message definition XML at: 5 https://github.com/mavlink/mavlink/tree/master/message_definitions/v1.0 7 The files can be imported into a gitbook to display the messages as HTML 9 The script runs on both Python2 and Python 3. The following libraries must be imported: lxml, requests, bs4. 11 The file is run in mavlink/doc/ with no arguments. It writes the files to /messages/ 14 import lxml.etree
as ET
16 from bs4
import BeautifulSoup
as bs
21 xsl_file_name =
"mavlink_to_html_table_gitbook.xsl" 22 xml_message_definitions_dir_name =
"../message_definitions/v1.0/" 24 output_dir =
"./messages/" 25 output_dir_html=output_dir+
"_html/" 26 if not os.path.exists(output_dir_html):
27 os.makedirs(output_dir_html)
31 index_file_name =
"README.md" 32 index_file_name = output_dir + index_file_name
35 with open(xsl_file_name,
'r') as content_file: 36 xsl_file = content_file.read() 37 xslt = ET.fromstring(xsl_file) 40 index_text=
"""<!-- THIS FILE IS AUTO-GENERATED (DO NOT UPDATE GITBOOK): https://github.com/mavlink/mavlink/blob/master/doc/mavlink_gitbook.py --> 41 # Dialects {#dialects} 43 MAVLink *dialects* are XML files that define *protocol-* and *vendor-specific* messages, enums and commands. 45 Dialects may *include* other MAVLink XML files. 46 A typical pattern is for a dialect to include [common.xml](../messages/common.md) (containing the *MAVLink standard definitions*), extending it with vendor or protocol specific messages. 47 While a dialect can include any other message definition, only only a single level of nesting is supported ([at time of writing](https://github.com/ArduPilot/pymavlink/pull/248)). 49 > **Note** Vendor forks of MAVLink may contain dialect messages that are not yet merged, and hence will not appear in this documentation. 51 The dialect files are stored alongside in separate XML files in [mavlink/message definitions](https://github.com/mavlink/mavlink/blob/master/message_definitions/). 53 The human-readable forms of the XML dialect files are linked below: 61 def remove_space_between_content_tags(matchobj):
62 stripped_string=matchobj.group(1).strip()
63 return '>%s<' % stripped_string
65 input_html=re.sub(
r'>(\s+?\w+?.*?)<', remove_space_between_content_tags, input_html,flags=re.DOTALL)
70 input_html=input_html.replace(
'.xml.md.unlikely',
'.md')
75 input_html=input_html.replace(
'xxx_space_xxx',
' ')
81 index=original_text.find(strip_text)
82 stripped_string=original_text
84 stripped_string = stripped_string[index:]
85 return stripped_string
89 print(
'FILENAME: %s' % filename)
90 insert_text=
'<!-- THIS FILE IS AUTO-GENERATED: https://github.com/mavlink/mavlink/blob/master/doc/mavlink_gitbook.py -->' 91 if filename ==
'common.xml':
93 # MAVLINK Common Message Set 95 The MAVLink *common* message set is defined in [common.xml](https://github.com/mavlink/mavlink/blob/master/message_definitions/v1.0/common.xml). 96 It contains the *standard* definitions that are managed by the MAVLink project. 98 The definitions cover functionality that is considered useful to most ground control stations and autopilots. 99 MAVLink-compatible systems are expected to use these definitions where possible (if an appropriate message exists) rather than rolling out variants in their own [dialects](../messages/README.md). 101 This topic is a human-readable form of [common.xml](https://github.com/mavlink/mavlink/blob/master/message_definitions/v1.0/common.xml). 103 elif filename ==
'ardupilotmega.xml':
105 # Dialect: ArduPilotMega 107 These messages define the ArduPilot specific message set, which is custom to [http://ardupilot.org](http://ardupilot.org). 109 This topic is a human-readable form of the XML definition file: [ardupilotmega.xml](https://github.com/mavlink/mavlink/blob/master/message_definitions/v1.0/ardupilotmega.xml). 111 > **Warning** The ArduPilot MAVLink fork of [ardupilotmega.xml](https://github.com/ArduPilot/mavlink/blob/master/message_definitions/v1.0/ardupilotmega.xml) may contain messages that have not yet been merged into this documentation. 114 insert_text+=
'\n# Dialect: %s' % filename.rsplit(
'.',1)[0]
115 insert_text+=
'\n\n*This is a human-readable form of the XML definition file: [%s](https://github.com/mavlink/mavlink/blob/master/message_definitions/v1.0/%s).*' % (filename, filename)
119 > **Note** MAVLink 2 messages have an ID > 255 and are marked up using **(MAVLink 2)** in their description. 121 <span id="mav2_extension_field"></span> 122 > **Note** MAVLink 2 extension fields that have been added to MAVLink 1 messages are displayed in blue. 131 insert_text+=
'\n\n{%% include "_html/%s.html" %%}' % filename[:-4]
132 input_html=insert_text+
'\n\n'+input_html
137 dialect_files = set()
139 for subdir, dirs, files
in os.walk(xml_message_definitions_dir_name):
142 if not file.endswith(
'.xml'):
144 xml_file_name = xml_message_definitions_dir_name+file
145 with open(xml_file_name,
'r') as content_file: 146 xml_file = content_file.read() 147 dom = ET.fromstring(xml_file) 148 transform = ET.XSLT(xslt) 152 soup=bs(
str(newdom),
"lxml")
153 prettyHTML=soup.prettify()
166 output_file_name_html = file.rsplit(
'.',1)[0]+
".html" 168 output_file_name_html_withdir = output_dir_html+output_file_name_html
169 print(
"Output filename (html): %s" % output_file_name_html)
171 with open(output_file_name_html_withdir,
'w')
as out:
172 out.write(prettyHTML)
176 output_file_name_prefix = file.rsplit(
'.',1)[0]
182 output_file_name_md_withdir = output_dir+output_file_name_prefix+
'.md' 183 print(
"Output filename (md): %s" % output_file_name_md_withdir)
185 with open(output_file_name_md_withdir,
'w')
as out:
186 out.write(markdown_text)
189 if not file==
'common.xml':
190 dialect_files.add(output_file_name_prefix)
192 for the_file
in sorted(dialect_files):
193 index_text+=
'\n* [%s.xml](%s.md)' % (the_file,the_file)
196 with open(index_file_name,
'w')
as content_file:
197 content_file.write(index_text)
def inject_top_level_docs(input_html, filename)
def fix_replace_space_marker(input_html)
def fix_content_in_tags(input_html)
BS puts each tag/content in its own line.
def fix_include_file_extension(input_html)
def strip_text_before_string(original_text, strip_text)