From 4f201e920f95c675904033c91cf6b616b0fdf870 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Mon, 3 Apr 2017 18:48:16 +0100 Subject: Add monospace paragraph to code example --- README.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'README.md') diff --git a/README.md b/README.md index 787ae3c..a30e36d 100644 --- a/README.md +++ b/README.md @@ -402,6 +402,28 @@ transform_document = mammoth.transforms.paragraph(transform_paragraph) mammoth.convert_to_html(fileobj, transform_document=transform_document) ``` +Or if you want paragraphs that have been explicitly set to use monospace fonts to represent code: + +```python +import mammoth.documents +import mammoth.transforms + +_monospace_fonts = set(["Courier New"]) + +def transform_paragraph(paragraph): + runs = mammoth.transforms.get_descendants_of_type(paragraph, mammoth.documents.Run) + if runs and all(run.font in _monospace_fonts for run in runs): + return paragraph.copy(style_id="code", style_name="Code") + else: + return paragraph + +convert_to_html( + fileobj, + transform_document=mammoth.transforms.paragraph(transform_paragraph), + style_map="p[style-name='Code'] => pre:separator('\n')", +) +``` + #### `mammoth.transforms.paragraph(transform_paragraph)` Returns a function that can be used as the `transform_document` argument. -- cgit v1.2.3