diff options
Diffstat (limited to 'mammoth')
| -rw-r--r-- | mammoth/__init__.py | 19 | ||||
| -rw-r--r-- | mammoth/docx/__init__.py | 10 | ||||
| -rw-r--r-- | mammoth/docx/files.py | 14 |
3 files changed, 35 insertions, 8 deletions
diff --git a/mammoth/__init__.py b/mammoth/__init__.py index 27cc580..e8d3a83 100644 --- a/mammoth/__init__.py +++ b/mammoth/__init__.py @@ -16,15 +16,28 @@ def convert_to_markdown(*args, **kwargs): return convert(*args, output_format="markdown", **kwargs) -def convert(fileobj, transform_document=None, id_prefix=None, include_embedded_style_map=_undefined, **kwargs): +def convert( + fileobj, + transform_document=None, + id_prefix=None, + include_embedded_style_map=_undefined, + external_file_access=_undefined, + **kwargs +): if include_embedded_style_map is _undefined: include_embedded_style_map = True + if transform_document is None: transform_document = lambda x: x + if include_embedded_style_map: kwargs["embedded_style_map"] = read_style_map(fileobj) + + if external_file_access is _undefined: + external_file_access = True + return options.read_options(kwargs).bind(lambda convert_options: - docx.read(fileobj).map(transform_document).bind(lambda document: + docx.read(fileobj, external_file_access=external_file_access).map(transform_document).bind(lambda document: conversion.convert_document_element_to_html( document, id_prefix=id_prefix, @@ -32,7 +45,7 @@ def convert(fileobj, transform_document=None, id_prefix=None, include_embedded_s ) ) ) - + def extract_raw_text(fileobj): return docx.read(fileobj).map(extract_raw_text_from_element) diff --git a/mammoth/docx/__init__.py b/mammoth/docx/__init__.py index 3848736..a0576cf 100644 --- a/mammoth/docx/__init__.py +++ b/mammoth/docx/__init__.py @@ -19,13 +19,14 @@ from ..zips import open_zip _empty_result = results.success([]) -def read(fileobj): +def read(fileobj, external_file_access=False): zip_file = open_zip(fileobj, "r") part_paths = _find_part_paths(zip_file) read_part_with_body = _part_with_body_reader( getattr(fileobj, "name", None), zip_file, part_paths=part_paths, + external_file_access=external_file_access, ) return results.combine([ @@ -134,7 +135,7 @@ def _read_document(zip_file, read_part_with_body, notes, comments, part_paths): ) -def _part_with_body_reader(document_path, zip_file, part_paths): +def _part_with_body_reader(document_path, zip_file, part_paths, external_file_access): content_types = _try_read_entry_or_default( zip_file, "[Content_Types].xml", @@ -156,7 +157,10 @@ def _part_with_body_reader(document_path, zip_file, part_paths): default=Numbering.EMPTY, ) - files = Files(None if document_path is None else os.path.dirname(document_path)) + files = Files( + None if document_path is None else os.path.dirname(document_path), + external_file_access=external_file_access, + ) def read_part(name, reader, default=_undefined): relationships = _read_relationships(zip_file, _find_relationships_path_for(name)) diff --git a/mammoth/docx/files.py b/mammoth/docx/files.py index 61cf8d5..e26a43f 100644 --- a/mammoth/docx/files.py +++ b/mammoth/docx/files.py @@ -11,10 +11,16 @@ except ImportError: class Files(object): - def __init__(self, base): + def __init__(self, base, external_file_access): self._base = base - + self._external_file_access = external_file_access + def open(self, uri): + if not self._external_file_access: + raise ExternalFileAccessIsDisabledError( + "could not open external image '{0}', external file access is disabled".format(uri) + ) + try: if _is_absolute(uri): return contextlib.closing(urlopen(uri)) @@ -34,3 +40,7 @@ def _is_absolute(url): class InvalidFileReferenceError(ValueError): pass + + +class ExternalFileAccessIsDisabledError(InvalidFileReferenceError): + pass |
