-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add command to convert markdown to html
Issue: AAH-3009
- Loading branch information
1 parent
98c7957
commit 21b181e
Showing
6 changed files
with
121 additions
and
1 deletion.
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 @@ | ||
Add command to convert markdown to html |
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
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
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,39 @@ | ||
import logging | ||
import os | ||
|
||
from galaxy_importer import exceptions as exc | ||
from galaxy_importer import __version__ | ||
from galaxy_importer.utils import markup | ||
|
||
default_logger = logging.getLogger(__name__) | ||
|
||
|
||
def convert_markdown(dirname, logger=None): | ||
"""Convert README.md to html. | ||
NOTE: README.md must exist in dirname | ||
:param dirname: directory of role. | ||
:param logger: Optional logger instance. | ||
:raises exc.ImporterError: On errors that fail the import process. | ||
:return: html markup of supplied README.md file | ||
""" | ||
logger = logger or default_logger | ||
logger.info(f"Converting markdown with galaxy-importer {__version__}") | ||
|
||
# Ensure that dirname exists. | ||
if not os.path.exists(dirname): | ||
raise exc.ImporterError(f"Path does not exist: {dirname}") | ||
|
||
return _convert_markdown(dirname, logger) | ||
|
||
|
||
def _convert_markdown(dirname, logger): | ||
doc_file = markup.get_readme_doc_file(dirname) | ||
if not doc_file: | ||
raise exc.ImporterError(f"Path does not contain README.md: {dirname}") | ||
else: | ||
logger.info(f"Processing {dirname}{doc_file.name}") | ||
return {"html": markup.get_html(doc_file)} |
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
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,51 @@ | ||
import logging | ||
import os | ||
import pytest | ||
import tempfile | ||
|
||
from galaxy_importer import exceptions as exc | ||
from galaxy_importer import markdown | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
README_MD = """ | ||
# my_readme | ||
Some generic readme | ||
""" | ||
|
||
README_HTML = """<h1>my_readme</h1> | ||
<p>Some generic readme</p>""" | ||
|
||
|
||
def test_convert_markdown(mocker): | ||
mocker.patch.object(markdown, "_convert_markdown") | ||
with tempfile.TemporaryDirectory() as tmp_dir: | ||
with open(os.path.join(tmp_dir, "README.md"), "w") as file: | ||
file.write(README_MD) | ||
markdown.convert_markdown(tmp_dir) | ||
assert markdown._convert_markdown.called | ||
|
||
|
||
def test_convert_markdown_return(): | ||
with tempfile.TemporaryDirectory() as tmp_dir: | ||
with open(os.path.join(tmp_dir, "README.md"), "w") as file: | ||
file.write(README_MD) | ||
data = markdown.convert_markdown(tmp_dir) | ||
assert isinstance(data, dict) | ||
assert "html" in data | ||
assert README_HTML == data["html"] | ||
|
||
|
||
def test_convert_markdown_dir_dne(): | ||
with tempfile.TemporaryDirectory() as tmp_dir: | ||
with pytest.raises(exc.ImporterError, match="does not exist"): | ||
markdown.convert_markdown(f"{tmp_dir}/does/not/exist") | ||
|
||
|
||
def test_convert_markdown_readme_dne(): | ||
with tempfile.TemporaryDirectory() as tmp_dir: | ||
with open(os.path.join(tmp_dir, "README.html"), "w") as file: | ||
file.write(README_MD) | ||
with pytest.raises(exc.ImporterError, match="Path does not contain README"): | ||
markdown.convert_markdown(tmp_dir) |