From 1483a864719cedabc0d6c6bcd98e0b2d9d3ce0c1 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 25 Jan 2015 17:06:54 +0000 Subject: Use prefix to generate IDs --- mammoth/__init__.py | 4 ++-- mammoth/conversion.py | 29 +++++++++++++++-------------- mammoth/documents.py | 4 ++-- tests/conversion_tests.py | 10 +++++----- tests/docx/document_xml_tests.py | 2 +- tests/mammoth_tests.py | 20 ++++++++++---------- 6 files changed, 35 insertions(+), 34 deletions(-) diff --git a/mammoth/__init__.py b/mammoth/__init__.py index b0ffa12..c7b305d 100644 --- a/mammoth/__init__.py +++ b/mammoth/__init__.py @@ -3,13 +3,13 @@ from . import docx, conversion, options, documents, images, underline __all__ = ["convert_to_html", "extract_raw_text", "images", "underline"] -def convert_to_html(fileobj, transform_document=None, generate_uniquifier=None, **kwargs): +def convert_to_html(fileobj, transform_document=None, id_prefix=None, **kwargs): if transform_document is None: transform_document = lambda x: x return docx.read(fileobj).map(transform_document).bind(lambda document: conversion.convert_document_element_to_html( document, - generate_uniquifier=generate_uniquifier, + id_prefix=id_prefix, **options.read_options(kwargs) ) ) diff --git a/mammoth/conversion.py b/mammoth/conversion.py index 6ef13c8..0eb3ccc 100644 --- a/mammoth/conversion.py +++ b/mammoth/conversion.py @@ -12,29 +12,29 @@ def convert_document_element_to_html(element, style_map=None, convert_image=None, convert_underline=None, - generate_uniquifier=None): + id_prefix=None): if style_map is None: style_map = [] - if generate_uniquifier is None: - generate_uniquifier = lambda: random.randint(0, 1000000000000000) + if id_prefix is None: + id_prefix = str(random.randint(0, 1000000000000000)) html_generator = HtmlGenerator() converter = DocumentConverter(style_map, convert_image=convert_image, convert_underline=convert_underline, - generate_uniquifier=generate_uniquifier) + id_prefix=id_prefix) converter.convert_element_to_html(element, html_generator,) html_generator.end_all() return results.Result(html_generator.html_string(), converter.messages) class DocumentConverter(object): - def __init__(self, style_map, convert_image, convert_underline, generate_uniquifier): + def __init__(self, style_map, convert_image, convert_underline, id_prefix): self.messages = [] self._style_map = style_map - self._uniquifier = generate_uniquifier() + self._id_prefix = id_prefix self._note_references = [] self._converters = { documents.Document: self._convert_document, @@ -147,11 +147,10 @@ class DocumentConverter(object): } def _convert_note_reference(self, note_reference, html_generator): - uid = self._note_uid(note_reference.note_id) html_generator.start("sup") html_generator.start("a", { - "href": "#{0}-{1}".format(note_reference.note_type, uid), - "id": "{0}-ref-{1}".format(note_reference.note_type, uid), + "href": "#" + self._note_html_id(note_reference), + "id": self._note_ref_html_id(note_reference), }) self._note_references.append(note_reference); note_number = len(self._note_references) @@ -160,12 +159,11 @@ class DocumentConverter(object): html_generator.end() def _convert_note(self, note, html_generator): - uid = self._note_uid(note.id) - html_generator.start("li", {"id": "{0}-{1}".format(note.note_type, uid)}) + html_generator.start("li", {"id": self._note_html_id(note)}) note_generator = HtmlGenerator() self._convert_elements_to_html(note.body, note_generator) note_generator.text(" ") - note_generator.start("a", {"href": "#{0}-ref-{1}".format(note.note_type, uid)}) + note_generator.start("a", {"href": "#" + self._note_ref_html_id(note)}) note_generator.text(_up_arrow) note_generator.end_all() html_generator.append(note_generator) @@ -200,8 +198,11 @@ class DocumentConverter(object): return default - def _note_uid(self, note_id): - return "{0}-{1}".format(self._uniquifier, note_id) + def _note_html_id(self, note): + return "{0}-{1}-{2}".format(self._id_prefix, note.note_type, note.note_id) + + def _note_ref_html_id(self, note): + return "{0}-{1}-ref-{2}".format(self._id_prefix, note.note_type, note.note_id) def _document_matcher_matches(matcher, element, element_type): diff --git a/mammoth/documents.py b/mammoth/documents.py index 9effce8..1adcc0d 100644 --- a/mammoth/documents.py +++ b/mammoth/documents.py @@ -67,7 +67,7 @@ def numbering_level(level_index, is_ordered): _NumberingLevel = dodge.data_class("NumberingLevel", ["level_index", "is_ordered"]) -note = Note = dodge.data_class("Note", ["note_type", "id", "body"]) +note = Note = dodge.data_class("Note", ["note_type", "note_id", "body"]) class Notes(object): @@ -93,6 +93,6 @@ def notes(notes_list): )) def _note_key(note): - return (note.note_type, note.id) + return (note.note_type, note.note_id) note_reference = NoteReference = dodge.data_class("NoteReference", ["note_type", "note_id"]) diff --git a/tests/conversion_tests.py b/tests/conversion_tests.py index 39c57cd..78fbce5 100644 --- a/tests/conversion_tests.py +++ b/tests/conversion_tests.py @@ -289,9 +289,9 @@ def footnote_reference_is_converted_to_superscript_intra_page_link(): footnote_reference = documents.note_reference("footnote", "4") result = convert_document_element_to_html( footnote_reference, - generate_uniquifier=lambda: 42 + id_prefix="doc-42" ) - assert_equal('[1]', result.value) + assert_equal('[1]', result.value) @istest @@ -308,10 +308,10 @@ def footnotes_are_included_after_the_main_body(): ) result = convert_document_element_to_html( document, - generate_uniquifier=lambda: 42 + id_prefix="doc-42" ) - expected_html = ('

Knock knock[1]

' + - '
  1. Who\'s there? ↑

') + expected_html = ('

Knock knock[1]

' + + '
  1. Who\'s there? ↑

') assert_equal(expected_html, result.value) diff --git a/tests/docx/document_xml_tests.py b/tests/docx/document_xml_tests.py index a0d29b9..a2eefaf 100644 --- a/tests/docx/document_xml_tests.py +++ b/tests/docx/document_xml_tests.py @@ -322,7 +322,7 @@ class ReadXmlElementTests(object): document = _read_and_get_document_xml_element(document_xml, footnote_elements=footnotes) footnote = document.notes.find_note("footnote", "4") - assert_equal("4", footnote.id) + assert_equal("4", footnote.note_id) assert isinstance(footnote.body[0], documents.Paragraph) @istest diff --git a/tests/mammoth_tests.py b/tests/mammoth_tests.py index 79ce50c..c5f2557 100644 --- a/tests/mammoth_tests.py +++ b/tests/mammoth_tests.py @@ -61,13 +61,13 @@ def word_tables_are_converted_to_html_tables(): def footnotes_are_appended_to_text(): # TODO: don't duplicate footnotes with multiple references expected_html = ('

Ouch' + - '[1].' + - '[2]

' + - '
  1. A tachyon walks into a bar. ↑

  2. ' + - '
  3. Fin. ↑

') + '[1].' + + '[2]

' + + '
  1. A tachyon walks into a bar. ↑

  2. ' + + '
  3. Fin. ↑

') with open(test_path("footnotes.docx"), "rb") as fileobj: - result = mammoth.convert_to_html(fileobj=fileobj, generate_uniquifier=lambda: 42) + result = mammoth.convert_to_html(fileobj=fileobj, id_prefix="doc-42") assert_equal([], result.messages) assert_equal(expected_html, result.value) @@ -75,13 +75,13 @@ def footnotes_are_appended_to_text(): @istest def endnotes_are_appended_to_text(): expected_html = ('

Ouch' + - '[1].' + - '[2]

' + - '
  1. A tachyon walks into a bar. ↑

  2. ' + - '
  3. Fin. ↑

') + '[1].' + + '[2]

' + + '
  1. A tachyon walks into a bar. ↑

  2. ' + + '
  3. Fin. ↑

') with open(test_path("endnotes.docx"), "rb") as fileobj: - result = mammoth.convert_to_html(fileobj=fileobj, generate_uniquifier=lambda: 42) + result = mammoth.convert_to_html(fileobj=fileobj, id_prefix="doc-42") assert_equal([], result.messages) assert_equal(expected_html, result.value) -- cgit v1.2.3