MCPcopy Create free account
hub / github.com/Python-Markdown/markdown / convertFile

Method convertFile

markdown/core.py:401–472  ·  view source on GitHub ↗

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,
    )

Source from the content-addressed store, hash-verified

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

Callers 1

markdownFromFileFunction · 0.95

Calls 2

convertMethod · 0.95
closeMethod · 0.45

Tested by

no test coverage detected