Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to output to file in CLI (#608) #609

Merged
merged 1 commit into from
Nov 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- [pull #605] Add support for Python 3.13, drop EOL 3.8
- [pull #607] Fix `middle-word-em` extra preventing strongs from being recognized (#606)
- [pull #609] Add option to output to file in CLI (#608)


## python-markdown2 2.5.1
Expand Down
7 changes: 6 additions & 1 deletion lib/markdown2.py
Original file line number Diff line number Diff line change
Expand Up @@ -3985,6 +3985,7 @@ def main(argv=None):
help="run internal self-tests (some doctests)")
parser.add_argument("--compare", action="store_true",
help="run against Markdown.pl as well (for testing)")
parser.add_argument('--output', type=str, help='output to a file instead of stdout')
parser.set_defaults(log_level=logging.INFO, compare=False,
encoding="utf-8", safe_mode=None, use_file_vars=False)
opts = parser.parse_args()
Expand Down Expand Up @@ -4059,7 +4060,11 @@ def main(argv=None):
extras=extras, link_patterns=link_patterns,
use_file_vars=opts.use_file_vars,
cli=True)
sys.stdout.write(html)
if opts.output:
with open(opts.output, 'w') as f:
f.write(html)
else:
sys.stdout.write(html)
if extras and "toc" in extras:
log.debug("toc_html: " +
str(html.toc_html.encode(sys.stdout.encoding or "utf-8", 'xmlcharrefreplace')))
Expand Down