forked from openvinotoolkit/open_model_zoo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ci/prepare-documentation.py: factor out the Markdown parsing code int…
…o a module I plan to add more scripts that reuse this logic. Another benefit of this is that it'll let us switch out the underlying Markdown parser if mistune proves unsatisfactory.
- Loading branch information
Roman Donchenko
committed
Apr 2, 2021
1 parent
3b769af
commit afabd42
Showing
2 changed files
with
67 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# Copyright (c) 2021 Intel Corporation | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import collections | ||
|
||
import mistune | ||
|
||
_parse_markdown = mistune.create_markdown(renderer=mistune.AstRenderer()) | ||
|
||
def _get_all_ast_nodes(ast_nodes): | ||
for node in ast_nodes: | ||
yield node | ||
if 'children' in node: | ||
# workaround for https://github.com/lepture/mistune/issues/269 | ||
if isinstance(node['children'], str): | ||
yield {'type': 'text', 'text': node['children']} | ||
else: | ||
yield from _get_all_ast_nodes(node['children']) | ||
|
||
def _get_text_from_ast(ast_nodes): | ||
def get_text_from_node(node): | ||
if node['type'] != 'text': | ||
raise RuntimeError(f'unsupported node type: {node["type"]}') | ||
return node['text'] | ||
|
||
return ''.join(map(get_text_from_node, ast_nodes)) | ||
|
||
ExternalReference = collections.namedtuple('ExternalReference', ['type', 'url']) | ||
|
||
class DocumentationPage: | ||
def __init__(self, markdown_text): | ||
self._ast = ast = _parse_markdown(markdown_text) | ||
|
||
self._title = None | ||
if ast and ast[0]['type'] == 'heading' and ast[0]['level'] == 1: | ||
self._title = _get_text_from_ast(ast[0]['children']) | ||
|
||
@property | ||
def title(self): | ||
return self._title | ||
|
||
def external_references(self): | ||
for node in _get_all_ast_nodes(self._ast): | ||
if node['type'] == 'image': | ||
yield ExternalReference('image', node['src']) | ||
elif node['type'] == 'link': | ||
yield ExternalReference('link', node['link']) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters