Skip to content

Commit

Permalink
Add support for more word fields in hyperlinks using the @ prefix.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Forbes committed Jun 26, 2017
1 parent 57ca197 commit 7e2b2dd
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 14 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 1.0.5
Add support for more word fields in hyperlinks. The syntax is `<a href='@CODE'>`. Currently only `FILENAME` is supported

## 1.0.4
Add support for text-alignment in paragraphs.

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
48 changes: 37 additions & 11 deletions tests/comparison/generate_report.py
Original file line number Diff line number Diff line change
@@ -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:]]
Expand All @@ -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))

Expand Down
Binary file modified tests/comparison/images/cross_references.html.html.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/comparison/images/cross_references.html.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions tests/docs/cross_references.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
<h2 id="MyRef">Some Text Here</h2>

<p>Reference: <a href="!MyRef">Should not appear in the word doc</a></p>

<p>Can use '@CODE' to insert document fields, or '!ref' to insert field REF codes</p>
<p>Invalid Code: <a href="@TEST">Nothing goes here</a></p>
<p>Document Name: <a href="@FILENAME">Filename Goes Here</a></p>

13 changes: 11 additions & 2 deletions wordinserter/renderers/com.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 7e2b2dd

Please sign in to comment.