mdpo.po2md package
Markdown files translator using PO files as reference.
- class mdpo.po2md.Po2Md(pofiles, ignore=frozenset({}), po_encoding=None, **kwargs)
Bases:
object
PO files to Markdown translator implementation.
This class is where all the translation process is carried out. If you are executing custom translation events, you may want to read the documentation about the properties of this class to properly control the internal state of the parser.
Example
If you want to translate all “Foo” messages as “Bar”, regardless of the content of the actual translation, you could do something like:
def transform_foo(self, block, text): if text == 'Foo': self.current_msgid = 'Bar' # self is Po2Md return False pofile_to_markdown( 'Foo', pofile_path, events={'text': transform_foo}, )
The public internal properties of this class are documented below:
- _aimg_title_inside_current_msgid
- _append_link_references()
- _codespan_backticks
- _codespan_inside_current_msgid
- _codespan_start_index
- _current_aspan_href
- _current_aspan_ref_target
- _current_aspan_text
- _current_imgspan
- _current_list_type
- _current_thead_aligns
- _current_wikilink_target
- _enterspan_replacer
- _escape_translation(text)
- _inside_aspan
- _inside_codeblock
- _inside_codespan
- _inside_hblock
- _inside_htmlblock
- _inside_indented_codeblock
- _inside_latexmath_display
- _inside_liblock
- _inside_liblock_first_p
- _inside_pblock
- _inside_quoteblock
- _leavespan_replacer
- _ol_marks
- _process_command(text)
- _save_current_line()
- _save_current_msgid()
- _saved_files_changed
- _translate_msgid(msgid, msgctxt, tcomment)
- _ul_marks
- bold_end_string
- bold_start_string
- code_end_string
- code_end_string_escaped
- code_start_string
- code_start_string_escaped
- command(mdpo_command, comment, original_command)
- command_aliases
- content
- current_msgid
The msgid being currently built for the next msgstr translation. Keep in mind that, if you are executing an event that will be followed by an span one (
enter_span
orexit_span
), the content of the msgid will change before translate it.- Type:
- disable
Indicates if the translator is currently disabled, which happens after a
<!-- mdpo-disable -->
command is found, before any subsecuents<!-- mdpo-enable -->
commands.- Type:
- disabled_entries
Disabled PO entries
- Type:
list(
polib.POEntry
)
- enable_next_block
Indicates if the next block will be translated when the translator is disabled
- Type:
- enter_block(block, details)
- enter_span(span, details)
- extensions
MD4C extensions used to parse the content. See all available in mdpo.md4c module.
- italic_end_string
- italic_end_string_escaped
- italic_start_string
- italic_start_string_escaped
- latexmath_display_end_string
- latexmath_display_start_string
- latexmath_end_string
- latexmath_start_string
- leave_block(block, details)
- leave_span(span, details)
- link_references
- output
- text(block, text)
- translate(filepath_or_content, save=None, md_encoding='utf-8')
- translated_entries
Translated PO entries
- Type:
list(
polib.POEntry
)
- translations
- translations_with_msgctxt
- wikilink_end_string
- wikilink_start_string
- wrapwidth
- mdpo.po2md.pofile_to_markdown(filepath_or_content, pofiles, ignore=frozenset({}), save=None, md_encoding='utf-8', po_encoding=None, command_aliases=None, wrapwidth=80, events=None, debug=False, **kwargs)
Translate Markdown content or file using PO files as reference.
This implementation reproduces the same valid Markdown output, given the provided AST, with replaced translations, but doesn’t rebuilds the same input format as Markdown is just a subset of HTML.
- Parameters:
filepath_or_content (str) – Markdown filepath or content to translate.
pofiles (str, list) – Glob or list of globs matching a set of PO files from where to extract messages to make the replacements translating strings.
ignore (list) – Paths of PO files to ignore. Useful when a glob does not fit your requirements indicating the files to extract content. Also, filename or a dirname can be defined without indicate the full path.
save (str) – Saves the output content in file whose path is specified at this parameter.
md_encoding (str) – Markdown content encoding.
po_encoding (str) – PO files encoding. If you need different encodings for each file, you must define it in the “Content-Type” field of each PO file metadata, in the form
"Content-Type: text/plain; charset=<ENCODING>\n"
.command_aliases (dict) – Mapping of aliases to use custom mdpo command names in comments. The
mdpo-
prefix in command names resolution is optional. For example, if you want to use<!-- mdpo-on -->
instead of<!-- mdpo-enable -->
, you can pass the dictionaries{"mdpo-on": "mdpo-enable"}
or{"mdpo-on": "enable"}
to this parameter.wrapwidth (int) – Maximum width used rendering the Markdown output.
events (dict) –
Preprocessing events executed during the translation process that can be used to customize the output. Takes list of functions as values. If one of these functions returns
False
, that part of the translation process is skipped bypo2md
. Available events are the next:enter_block(self, block, details)
: Executed when the parsing of a Markdown block starts.leave_block(self, block, details)
: Executed when the parsing of a Markdown block ends.enter_span(self, span, details)
: Executed when the parsing of a Markdown span starts.leave_span(self, span, details)
: Executed when the parsing of a Markdown span ends.text(self, block, text)
: Executed when the parsing of text starts.command(self, mdpo_command, comment, original command)
: Executed when a mdpo HTML command is found.msgid(self, msgid, msgstr, msgctxt, tcomment, flags)
: Executed when a msgid is going to be replaced.link_reference(self, target, href, title)
: Executed when each reference link is being written in the output (at the end of the translation process).
You can also define the location of these functions by strings with the syntax
path/to/file.py::function_name
.debug (bool) – Add events displaying all parsed elements in the translation process.
**kwargs – Extra arguments passed to
mdpo.po2md.Po2Md
constructor.
- Returns:
Markdown output file with translated content.
- Return type: