Read Markdown text from a file or stream and write HTML output to a file or stream. Decodes the input file using the provided encoding (defaults to `utf-8`), passes the file content to markdown, and outputs the HTML to either the provided stream or the file with pro
(
self,
input: str | BinaryIO | None = None,
output: str | BinaryIO | None = None,
encoding: str | None = None,
)
| 408 | return output.strip() |
| 409 | |
| 410 | def convertFile( |
| 411 | self, |
| 412 | input: str | BinaryIO | None = None, |
| 413 | output: str | BinaryIO | None = None, |
| 414 | encoding: str | None = None, |
| 415 | ) -> Markdown: |
| 416 | """ |
| 417 | Read Markdown text from a file or stream and write HTML output to a file or stream. |
| 418 | |
| 419 | Decodes the input file using the provided encoding (defaults to `utf-8`), |
| 420 | passes the file content to markdown, and outputs the HTML to either |
| 421 | the provided stream or the file with provided name, using the same |
| 422 | encoding as the source file. The |
| 423 | [`xmlcharrefreplace`](https://docs.python.org/3/library/codecs.html#error-handlers) |
| 424 | error handler is used when encoding the output. |
| 425 | |
| 426 | **Note:** This is the only place that decoding and encoding of Unicode |
| 427 | takes place in Python-Markdown. (All other code is Unicode-in / |
| 428 | Unicode-out.) |
| 429 | |
| 430 | Arguments: |
| 431 | input: File object or path. Reads from `stdin` if `None`. |
| 432 | output: File object or path. Writes to `stdout` if `None`. |
| 433 | encoding: Encoding of input and output files. Defaults to `utf-8`. |
| 434 | |
| 435 | !!! warning |
| 436 | The Python-Markdown library does ***not*** sanitize its HTML output. |
| 437 | As `Markdown.convertFile` writes directly to the file system, there is no |
| 438 | easy way to sanitize the output from Python code. Therefore, it is |
| 439 | recommended that the `Markdown.convertFile` method not be used on input |
| 440 | from an untrusted source. For more information see [Sanitizing HTML |
| 441 | Output](../../sanitization.md). |
| 442 | |
| 443 | """ |
| 444 | |
| 445 | encoding = encoding or "utf-8" |
| 446 | |
| 447 | # Read the source |
| 448 | if input: |
| 449 | if isinstance(input, str): |
| 450 | input_file = open(input, mode="r", encoding=encoding) |
| 451 | else: |
| 452 | input_file = codecs.getreader(encoding)(input) |
| 453 | text = input_file.read() |
| 454 | input_file.close() |
| 455 | else: |
| 456 | text = sys.stdin.read() |
| 457 | |
| 458 | text = text.lstrip('\ufeff') # remove the byte-order mark |
| 459 | |
| 460 | # Convert |
| 461 | html = self.convert(text) |
| 462 | |
| 463 | # Write to file or stdout |
| 464 | if output: |
| 465 | if isinstance(output, str): |
| 466 | output_file = codecs.open(output, "w", |
| 467 | encoding=encoding, |
no test coverage detected