MCPcopy Index your code
hub / github.com/Python-Markdown/markdown / convertFile

Method convertFile

markdown/core.py:394–465  ·  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

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

Callers 1

markdownFromFileFunction · 0.95

Calls 2

convertMethod · 0.95
closeMethod · 0.45

Tested by

no test coverage detected