diff --git a/CHANGELOG.md b/CHANGELOG.md index e3bdd0c..e0b1ed4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +## 1.0.5 +Add support for more word fields in hyperlinks. The syntax is ``. Currently only `FILENAME` is supported + ## 1.0.4 Add support for text-alignment in paragraphs. diff --git a/setup.py b/setup.py index 3eca00b..963f524 100644 --- a/setup.py +++ b/setup.py @@ -21,7 +21,7 @@ setup( name='wordinserter', - version='1.0.4', + version='1.0.5', packages=find_packages(), url='https://github.com/orf/wordinserter', license='MIT', diff --git a/tests/comparison/generate_report.py b/tests/comparison/generate_report.py index d70c6ed..dc89a9e 100644 --- a/tests/comparison/generate_report.py +++ b/tests/comparison/generate_report.py @@ -1,25 +1,30 @@ import pathlib -import shutil import subprocess import sys +import tempfile +import dhash from selenium import webdriver -from wand.color import Color from wand.image import Image image_directory = pathlib.Path("images") -if image_directory.exists(): - shutil.rmtree(str(image_directory)) - # If the directory is open in explorer it won't be removed. if not image_directory.exists(): image_directory.mkdir() +temp_directory = pathlib.Path(tempfile.mkdtemp()) # [(file name, word image, html image)] results = [] + +def is_same_image(img1, img2): + current_hash = dhash.dhash_int(img1) + old_hash = dhash.dhash_int(img2) + return dhash.get_num_bits_different(current_hash, old_hash) == 0 + + if __name__ == "__main__": if len(sys.argv) != 1: file_names = [pathlib.Path(p) for p in sys.argv[1:]] @@ -35,20 +40,41 @@ print('- Handling {0}'.format(file.absolute())) word_save = image_directory / (file.name + '.png') + word_save_temp = temp_directory / (file.name + '.png') + browser_save = image_directory / (file.name + '.html.png') + browser_save_temp = temp_directory / (file.name + '.html.png') + + subprocess.call(['wordinserter', str(file), '--close', '--save={save}'.format(save=word_save_temp)]) - subprocess.call(['wordinserter', str(file), '--close', '--save={save}'.format(save=word_save)]) + if word_save.exists(): + with Image(filename=str(word_save)) as current_word_image,\ + Image(filename=str(word_save_temp)) as new_word_img: + if not is_same_image(current_word_image, new_word_img): + word_save.write_bytes(word_save_temp.read_bytes()) + else: + print('{0} and {1} are the same, not overwriting'.format(word_save, word_save_temp)) + else: + word_save.write_bytes(word_save_temp.read_bytes()) browser.get('file://{0}'.format(file.absolute())) browser.execute_script("document.body.style.margin = '0px'") browser.execute_script("document.body.style.padding = '10px'") browser.execute_script("document.body.style.display = 'inline-block'") body_size = browser.find_element_by_tag_name('body').size - browser.save_screenshot(str(browser_save)) - - with Image(filename=str(browser_save)) as browser_img: - browser_img.crop(**body_size) - browser_img.save(filename=str(browser_save)) + browser.save_screenshot(str(browser_save_temp)) + + with Image(filename=str(browser_save_temp)) as new_browser_img: + new_browser_img.crop(**body_size) + + if browser_save.exists(): + with Image(filename=str(browser_save)) as current_img: + if not is_same_image(current_img, new_browser_img): + new_browser_img.save(filename=str(browser_save)) + else: + print('{0} and {1} are the same, not overwriting'.format(browser_save, browser_save_temp)) + else: + new_browser_img.save(filename=str(browser_save)) results.append((file.name, word_save, browser_save)) diff --git a/tests/comparison/images/cross_references.html.html.png b/tests/comparison/images/cross_references.html.html.png index 9601922..06dc271 100644 Binary files a/tests/comparison/images/cross_references.html.html.png and b/tests/comparison/images/cross_references.html.html.png differ diff --git a/tests/comparison/images/cross_references.html.png b/tests/comparison/images/cross_references.html.png index 3415ccb..5c12694 100644 Binary files a/tests/comparison/images/cross_references.html.png and b/tests/comparison/images/cross_references.html.png differ diff --git a/tests/docs/cross_references.html b/tests/docs/cross_references.html index 8a932dc..b514e93 100644 --- a/tests/docs/cross_references.html +++ b/tests/docs/cross_references.html @@ -1,3 +1,8 @@

Some Text Here

Reference: Should not appear in the word doc

+ +

Can use '@CODE' to insert document fields, or '!ref' to insert field REF codes

+

Invalid Code: Nothing goes here

+

Document Name: Filename Goes Here

+ diff --git a/wordinserter/renderers/com.py b/wordinserter/renderers/com.py index 0f76a12..f2bf325 100644 --- a/wordinserter/renderers/com.py +++ b/wordinserter/renderers/com.py @@ -281,11 +281,20 @@ def hyperlink(self, op: HyperLink): if op.location.startswith('#'): self.document.Hyperlinks.Add(Anchor=rng, TextToDisplay="", SubAddress=op.location.replace('#', '', 1)) - elif op.location.startswith('!'): + elif op.location.startswith('!') or op.location.startswith('@'): + if op.location.startswith('!'): + text = "REF {} \h".format(op.location.replace('!', '', 1)) + else: + text = op.location.replace('@', '', 1) + code = text.split(' ')[0] + # Whitelist document codes. + if code not in {'FILENAME'}: + return + field = self.document.Fields.Add( Range=rng, Type=self.constants.wdFieldEmpty, - Text="REF {} \h".format(op.location.replace('!', '', 1)), + Text=text, PreserveFormatting=True ) # When inserting fields, the cursor stays at the beginning, select it so it collapses to the end of it