Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix referenced image links #176

Merged
merged 1 commit into from
Nov 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 32 additions & 23 deletions mdpo/md2po/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,9 @@ def enter_span(self, span, details):
# save the index char of the opening backtick
self._codespan_start_index = len(self._current_msgid) - 1
elif span is md4c.SpanType.IMG:
if self._link_references is None:
self._link_references = parse_link_references(self.content)

self._current_imgspan['src'] = details['src'][0][1]
self._current_imgspan['title'] = '' if not details['title'] \
else details['title'][0][1]
Expand Down Expand Up @@ -784,30 +787,36 @@ def leave_span(self, span, details):
)
self._codespan_backticks = None
elif span is md4c.SpanType.IMG:
# TODO: refactor with getattr? Currently getting next error
# getattr(self, target_varname) += '![{}]({}'.format(
# SyntaxError: cannot assign to function call

if not self._inside_aspan:
self._current_msgid += '![{}]({}'.format(
self._current_imgspan['text'],
self._current_imgspan['src'],
)
title = self._current_imgspan['title']
if title:
self._current_msgid += f' "{title}"'
self._current_msgid += ')'
self._current_imgspan = {}
referenced_target, imgspan_title = (None, None)
imgspan_src = details['src'][0][1]
if details['title']:
imgspan_title = details['title'][0][1]
for target, href, title in self._link_references:
if href == imgspan_src and title == imgspan_title:
referenced_target = target
break
else:
self._current_aspan_text += '![{}]({}'.format(
self._current_imgspan['text'],
self._current_imgspan['src'],
)
title = self._current_imgspan['title']
if title:
self._current_aspan_text += f' "{title}"'
self._current_aspan_text += ')'
self._current_imgspan = {}
for target, href, _ in self._link_references:
if href == imgspan_src:
referenced_target = target
break

alt_text = self._current_imgspan['text']
img_markup = f'![{alt_text}]'
if referenced_target:
img_markup += f'[{referenced_target}]'
else:
img_markup += f'({imgspan_src}'
if imgspan_title:
img_markup += f' "{imgspan_title}"'
img_markup += ')'

self._current_imgspan = {}

if self._inside_aspan:
self._current_aspan_text += img_markup
else:
self._current_msgid += img_markup
elif span is md4c.SpanType.U:
self._inside_uspan = False

Expand Down
62 changes: 36 additions & 26 deletions mdpo/po2md/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,9 @@ def enter_span(self, span, details):
self._codespan_start_index = len(self._current_msgid)-1
self._codespan_inside_current_msgid = True
elif span is md4c.SpanType.IMG:
if self._link_references is None:
self._link_references = parse_link_references(self.content)

self._current_imgspan['title'] = '' if not details['title'] \
else details['title'][0][1]
self._current_imgspan['src'] = details['src'][0][1]
Expand Down Expand Up @@ -794,30 +797,37 @@ def leave_span(self, span, details):
)
self._codespan_backticks = None
elif span is md4c.SpanType.IMG:
# TODO: refactor with getattr? Currently getting next error
# getattr(self, target_varname) += '![{}]({}'.format(
# SyntaxError: cannot assign to function call

if not self._inside_aspan:
self._current_msgid += '![{}]({}'.format(
self._current_imgspan['text'],
self._current_imgspan['src'],
)
if self._current_imgspan['title']:
title = self._current_imgspan['title']
self._current_msgid += f' "{title}"'
self._current_msgid += ')'
referenced_target, imgspan_title = (None, None)
imgspan_src = details['src'][0][1]
if details['title']:
imgspan_title = details['title'][0][1]
for target, href, title in self._link_references:
if href == imgspan_src and title == imgspan_title:
referenced_target = target
break
else:
self._current_aspan_text += '![{}]({}'.format(
self._current_imgspan['text'],
self._current_imgspan['src'],
)
if self._current_imgspan['title']:
title = self._current_imgspan['title']
self._current_aspan_text += f' "{title}"'
self._current_aspan_text += ')'
for target, href, _ in self._link_references:
if href == imgspan_src:
referenced_target = target
break

alt_text = self._current_imgspan['text']
img_markup = f'![{alt_text}]'
if referenced_target:
img_markup += f'[{referenced_target}]'
else:
img_markup += f'({imgspan_src}'
if imgspan_title:
img_markup += f' "{imgspan_title}"'
img_markup += ')'

self._current_imgspan = {}

if self._inside_aspan:
self._current_aspan_text += img_markup
else:
self._current_msgid += img_markup

def text(self, block, text):
# raise 'text' event
if raise_skip_event(
Expand Down Expand Up @@ -891,7 +901,7 @@ def _append_link_references(self):
# 'link_reference' event
pre_events = self.events.get('link_reference')

_references_added = [] # don't repeat references
added_references = [] # don't repeat references
for target, href, title in self._link_references:
if pre_events:
skip = False
Expand All @@ -901,18 +911,18 @@ def _append_link_references(self):
if skip:
continue

href_part = '{}{}'.format(
href_title = '{}{}'.format(
f' {href}' if href else '',
f' "{title}"' if title else '',
)
if href_part in _references_added:
if href_title in added_references:
continue

msgid = '{}{}'.format(f'[{target}]:', href_part)
msgid = '{}{}'.format(f'[{target}]:', href_title)
self._outputlines.append(
self._translate_msgid(msgid, None, None),
)
_references_added.append(href_part)
added_references.append(href_title)
self._outputlines.append('')

def translate(
Expand Down
13 changes: 8 additions & 5 deletions test/test_md2po/extract-examples/markuptext/images.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@

![Hail Tux!](/assets/images/tux.png "Use linux, don't be crazy")

![Alt text][id]

[id]: url/to/image "Optional title attribute"
![Alt text][1]

My ![foo bar](/path/to/train.jpg "title")

[![Image inside inline link](https://image.ext)](https://link.ext)

[![Image inside referenced link](https://image.ext)][1]
[![Image inside referenced link](https://image.ext)][2]

[![Referenced link image inside referenced link][3]][4]

[1]: https://link-1.ext
[1]: url/to/image "Optional title attribute"
[2]: https://link-2.ext
[3]: https://image-3.ext
[4]: https://link-4.ext 'Referenced link image with title'
23 changes: 17 additions & 6 deletions test/test_md2po/extract-examples/markuptext/images.md.expect.po
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ msgstr ""
msgid "![Hail Tux!](/assets/images/tux.png \"Use linux, don't be crazy\")"
msgstr ""

msgid "![Alt text](url/to/image \"Optional title attribute\")"
msgid "![Alt text][1]"
msgstr ""

msgid "My ![foo bar](/path/to/train.jpg \"title\")"
Expand All @@ -20,13 +20,24 @@ msgstr ""
msgid "[![Image inside inline link](https://image.ext)](https://link.ext)"
msgstr ""

msgid "[![Image inside referenced link](https://image.ext)][1]"
msgid "[![Image inside referenced link](https://image.ext)][2]"
msgstr ""

msgid "[![Referenced link image inside referenced link][3]][4]"
msgstr ""

#, fuzzy
msgid "[1]: url/to/image \"Optional title attribute\""
msgstr "[1]: url/to/image \"Optional title attribute\""

#, fuzzy
msgid "[2]: https://link-2.ext"
msgstr "[2]: https://link-2.ext"

#, fuzzy
msgid "[id]: url/to/image \"Optional title attribute\""
msgstr "[id]: url/to/image \"Optional title attribute\""
msgid "[3]: https://image-3.ext"
msgstr "[3]: https://image-3.ext"

#, fuzzy
msgid "[1]: https://link-1.ext"
msgstr "[1]: https://link-1.ext"
msgid "[4]: https://link-4.ext \"Referenced link image with title\""
msgstr "[4]: https://link-4.ext \"Referenced link image with title\""
15 changes: 10 additions & 5 deletions test/test_po2md/translate-examples/images.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,19 @@

![Hail Tux!](/assets/images/tux.png "Use linux, don't be crazy")

![Alt text][id]

[id]: url/to/image "Optional title attribute"
![Alt text][1]

My ![foo bar](/path/to/train.jpg "title")

[![Image inside inline link](https://image.ext)](https://link.ext)

[![Image inside referenced link](https://image.ext)][1]
[![Image inside referenced link](https://image.ext)][2]

[![Image inside referenced link](https://image.ext)][2]

[![Referenced link image inside referenced link][3]][4]

[1]: https://link-1.ext
[1]: url/to/image "Optional title attribute"
[2]: https://link-2.ext
[3]: https://image-3.ext
[4]: https://link-4.ext 'Referenced link image with title'
14 changes: 10 additions & 4 deletions test/test_po2md/translate-examples/images.md.expect.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,19 @@

![Hail Tux!](/assets/images/tux.png "Usa linux, no seas loco")

![Texto alternativo](url/to/imagen "Atributo título opcional")
![Texto alternativo][1]

Mi ![foo bar](/path/to/tren.jpg "título")

[![Imagen dentro de link en línea](https://image.ext)](https://enlace.ext)

[![Imagen dentro de link referenciado](https://imagen.ext)][1]
[![Imagen dentro de link referenciado](https://imagen.ext)][2]

[id]: url/a/imagen "Título de atributo opcional"
[1]: https://enlace-1.ext
[![Imagen dentro de link referenciado](https://imagen.ext)][2]

[![Imagen con link referenciado dentro de link referenciado][3]][4]

[1]: url/a/imagen "Título de atributo opcional"
[2]: https://enlace-2.ext
[3]: https://imagen-3.ext
[4]: https://enlace-4.ext "Link de imagen referenciado con título"
25 changes: 17 additions & 8 deletions test/test_po2md/translate-examples/images.po
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,29 @@ msgstr "![](/assets/favicon.ico)"
msgid "![Hail Tux!](/assets/images/tux.png \"Use linux, don't be crazy\")"
msgstr "![Hail Tux!](/assets/images/tux.png \"Usa linux, no seas loco\")"

msgid "![Alt text](url/to/image \"Optional title attribute\")"
msgstr "![Texto alternativo](url/to/imagen \"Atributo título opcional\")"
msgid "![Alt text][1]"
msgstr "![Texto alternativo][1]"

msgid "My ![foo bar](/path/to/train.jpg \"title\")"
msgstr "Mi ![foo bar](/path/to/tren.jpg \"título\")"

msgid "[![Image inside inline link](https://image.ext)](https://link.ext)"
msgstr "[![Imagen dentro de link en línea](https://image.ext)](https://enlace.ext)"

msgid "[![Image inside referenced link](https://image.ext)][1]"
msgstr "[![Imagen dentro de link referenciado](https://imagen.ext)][1]"
msgid "[![Image inside referenced link](https://image.ext)][2]"
msgstr "[![Imagen dentro de link referenciado](https://imagen.ext)][2]"

msgid "[id]: url/to/image \"Optional title attribute\""
msgstr "[id]: url/a/imagen \"Título de atributo opcional\""
msgid "[![Referenced link image inside referenced link][3]][4]"
msgstr "[![Imagen con link referenciado dentro de link referenciado][3]][4]"

msgid "[1]: https://link-1.ext"
msgstr "[1]: https://enlace-1.ext"
msgid "[1]: url/to/image \"Optional title attribute\""
msgstr "[1]: url/a/imagen \"Título de atributo opcional\""

msgid "[2]: https://link-2.ext"
msgstr "[2]: https://enlace-2.ext"

msgid "[3]: https://image-3.ext"
msgstr "[3]: https://imagen-3.ext"

msgid "[4]: https://link-4.ext \"Referenced link image with title\""
msgstr "[4]: https://enlace-4.ext \"Link de imagen referenciado con título\""