From 0c2c1e7a096b16c232b25fd635e2e4c54b743cc0 Mon Sep 17 00:00:00 2001 From: Arijit Basu Date: Mon, 12 Feb 2024 11:59:04 +0530 Subject: [PATCH] Some fixes (#81) * Some fixes - urllib.quote_plus the url attributes - Match link domain more precisely. - Image height or width can be individually specified * 0.17.0 -> 0.18.0 * Fix tests * urllib.quote_plus -> html.escape * Minor * Escape doc * escape land * Escape recursively * Review changes --- CHANGELOG.rst | 6 ++ setup.cfg | 2 +- setup.py | 14 ++-- tests/data/html/audio-is_renderable.html | 2 +- tests/data/html/code_block-is_renderable.html | 2 +- tests/data/html/code_block.html | 2 +- tests/data/html/data_attributes.html | 2 +- tests/data/html/document-is_renderable.html | 2 +- tests/data/html/document-pdf.html | 2 +- tests/data/html/document-sketch.html | 2 +- .../data/html/featuredimage-height_width.html | 2 +- .../html/featuredimage-is_renderable.html | 2 +- tests/data/html/featuredimage-mime_type.html | 2 +- .../html/featuredimage-missing_caption.html | 2 +- tests/data/html/featuredimage-no_caption.html | 2 +- tests/data/html/featuredimage.html | 2 +- tests/data/html/heading.html | 2 +- tests/data/html/image-height_width.html | 2 +- tests/data/html/image-is_renderable.html | 2 +- tests/data/html/image-mime_type.html | 2 +- tests/data/html/image-missing_caption.html | 2 +- tests/data/html/image-no_caption.html | 2 +- tests/data/html/image.html | 2 +- tests/data/html/ordered_list.html | 2 +- tests/data/json/image.json | 7 +- tests/test_transform.py | 24 +++++-- tiptapy/__init__.py | 64 ++++++++++++------- tiptapy/image.py | 20 +++--- tiptapy/macros.py | 48 +++++++------- tiptapy/templates/codeBlock.html | 2 +- tiptapy/templates/code_block.html | 2 +- tiptapy/templates/doc.html | 4 +- tiptapy/templates/embed.html | 2 +- tiptapy/templates/extras/audio.html | 5 +- tiptapy/templates/extras/document.html | 2 +- tiptapy/templates/extras/featuredimage.html | 6 +- tiptapy/templates/image.html | 6 +- tiptapy/templates/text.html | 4 +- 38 files changed, 154 insertions(+), 106 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 281a8ce..c5320d8 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -2,6 +2,12 @@ History ======= +0.18.0 (2024-02-09) +------------------- +- html.escape all the attributes +- Match link domain more precisely. +- Image height or width can be individually specified + 0.16.0 (2023-03-14) ------------------- * Support for new tiptap versions that uses camelCase over snake_case diff --git a/setup.cfg b/setup.cfg index 9bdfaf0..e682e80 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,5 +1,5 @@ [bumpversion] -current_version = 0.17.0 +current_version = 0.18.0 commit = True tag = True diff --git a/setup.py b/setup.py index b084b57..1b9efb3 100644 --- a/setup.py +++ b/setup.py @@ -1,10 +1,8 @@ - -from setuptools import setup, find_packages - +from setuptools import find_packages, setup setup( name="tiptapy", - version='0.17.0', # TODO: why bumpversion works only for single quotes? + version="0.18.0", # TODO: why bumpversion works only for single quotes? url="https://github.com/scrolltech/tiptapy", description="Library that generates HTML output from JSON export of tiptap editor", long_description=open("README.md").read(), @@ -13,7 +11,13 @@ author="Shekhar Tiwatne", author_email="pythonic@gmail.com", license="http://www.opensource.org/licenses/mit-license.php", - package_data={"tiptapy": ["templates/*.html", "templates/extras/*.html", "templates/marks/*.html"]}, + package_data={ + "tiptapy": [ + "templates/*.html", + "templates/extras/*.html", + "templates/marks/*.html", + ] + }, classifiers=[ "Programming Language :: Python :: 3", "License :: OSI Approved :: MIT License", diff --git a/tests/data/html/audio-is_renderable.html b/tests/data/html/audio-is_renderable.html index 9ee1d5e..184f0af 100644 --- a/tests/data/html/audio-is_renderable.html +++ b/tests/data/html/audio-is_renderable.html @@ -1 +1 @@ -

Quick brown fox jumps over the lazy dog.

+

Quick brown fox jumps over the lazy dog.

\ No newline at end of file diff --git a/tests/data/html/code_block-is_renderable.html b/tests/data/html/code_block-is_renderable.html index 9ee1d5e..184f0af 100644 --- a/tests/data/html/code_block-is_renderable.html +++ b/tests/data/html/code_block-is_renderable.html @@ -1 +1 @@ -

Quick brown fox jumps over the lazy dog.

+

Quick brown fox jumps over the lazy dog.

\ No newline at end of file diff --git a/tests/data/html/code_block.html b/tests/data/html/code_block.html index 7f121d3..b5aaefd 100644 --- a/tests/data/html/code_block.html +++ b/tests/data/html/code_block.html @@ -40,4 +40,4 @@ num1 = 54 num2 = 24 -print("The L.C.M. is", compute_lcm(num1, num2))

Readability counts.

Zen of Python By Tom Peters

+print("The L.C.M. is", compute_lcm(num1, num2))

Readability counts.

Zen of Python By Tom Peters

\ No newline at end of file diff --git a/tests/data/html/data_attributes.html b/tests/data/html/data_attributes.html index 96b2849..ed202d1 100644 --- a/tests/data/html/data_attributes.html +++ b/tests/data/html/data_attributes.html @@ -1 +1 @@ -
Sleepy Kitten
Cute Kitty
Happy Kitten
New Kitty
\ No newline at end of file +
Sleepy Kitten
Cute Kitty
Happy Kitten
New Kitty
\ No newline at end of file diff --git a/tests/data/html/document-is_renderable.html b/tests/data/html/document-is_renderable.html index 9ee1d5e..184f0af 100644 --- a/tests/data/html/document-is_renderable.html +++ b/tests/data/html/document-is_renderable.html @@ -1 +1 @@ -

Quick brown fox jumps over the lazy dog.

+

Quick brown fox jumps over the lazy dog.

\ No newline at end of file diff --git a/tests/data/html/document-pdf.html b/tests/data/html/document-pdf.html index 77c3e20..566846d 100644 --- a/tests/data/html/document-pdf.html +++ b/tests/data/html/document-pdf.html @@ -16,4 +16,4 @@
  • Download
  • -
    This is pdf caption.

    Document Test

    +
    This is pdf caption.

    Document Test

    \ No newline at end of file diff --git a/tests/data/html/document-sketch.html b/tests/data/html/document-sketch.html index 8eb2241..085580c 100644 --- a/tests/data/html/document-sketch.html +++ b/tests/data/html/document-sketch.html @@ -16,4 +16,4 @@
  • Download
  • -
    Above URL may result into 404 :)

    Document Test

    +
    Above URL may result into 404 :)

    Document Test

    \ No newline at end of file diff --git a/tests/data/html/featuredimage-height_width.html b/tests/data/html/featuredimage-height_width.html index 21e14b9..d14ea8d 100644 --- a/tests/data/html/featuredimage-height_width.html +++ b/tests/data/html/featuredimage-height_width.html @@ -1 +1 @@ - \ No newline at end of file + \ No newline at end of file diff --git a/tests/data/html/featuredimage-is_renderable.html b/tests/data/html/featuredimage-is_renderable.html index 9ee1d5e..184f0af 100644 --- a/tests/data/html/featuredimage-is_renderable.html +++ b/tests/data/html/featuredimage-is_renderable.html @@ -1 +1 @@ -

    Quick brown fox jumps over the lazy dog.

    +

    Quick brown fox jumps over the lazy dog.

    \ No newline at end of file diff --git a/tests/data/html/featuredimage-mime_type.html b/tests/data/html/featuredimage-mime_type.html index 9863df0..397aed0 100644 --- a/tests/data/html/featuredimage-mime_type.html +++ b/tests/data/html/featuredimage-mime_type.html @@ -1 +1 @@ - + \ No newline at end of file diff --git a/tests/data/html/featuredimage-missing_caption.html b/tests/data/html/featuredimage-missing_caption.html index 8422fe7..01c67be 100644 --- a/tests/data/html/featuredimage-missing_caption.html +++ b/tests/data/html/featuredimage-missing_caption.html @@ -1 +1 @@ - + \ No newline at end of file diff --git a/tests/data/html/featuredimage-no_caption.html b/tests/data/html/featuredimage-no_caption.html index 8422fe7..01c67be 100644 --- a/tests/data/html/featuredimage-no_caption.html +++ b/tests/data/html/featuredimage-no_caption.html @@ -1 +1 @@ - + \ No newline at end of file diff --git a/tests/data/html/featuredimage.html b/tests/data/html/featuredimage.html index 946f713..35f7501 100644 --- a/tests/data/html/featuredimage.html +++ b/tests/data/html/featuredimage.html @@ -1 +1 @@ - + \ No newline at end of file diff --git a/tests/data/html/heading.html b/tests/data/html/heading.html index 54b327a..e1ef8d9 100644 --- a/tests/data/html/heading.html +++ b/tests/data/html/heading.html @@ -1 +1 @@ -

    This is a
    word

    +

    This is a
    word

    \ No newline at end of file diff --git a/tests/data/html/image-height_width.html b/tests/data/html/image-height_width.html index 0583d79..8c7d3a1 100644 --- a/tests/data/html/image-height_width.html +++ b/tests/data/html/image-height_width.html @@ -1 +1 @@ -
    Sleepy Kitten
    Cute Kitty
    \ No newline at end of file +
    Sleepy Kitten
    Cute Kitty
    \ No newline at end of file diff --git a/tests/data/html/image-is_renderable.html b/tests/data/html/image-is_renderable.html index 9ee1d5e..184f0af 100644 --- a/tests/data/html/image-is_renderable.html +++ b/tests/data/html/image-is_renderable.html @@ -1 +1 @@ -

    Quick brown fox jumps over the lazy dog.

    +

    Quick brown fox jumps over the lazy dog.

    \ No newline at end of file diff --git a/tests/data/html/image-mime_type.html b/tests/data/html/image-mime_type.html index 661b14b..1d70420 100644 --- a/tests/data/html/image-mime_type.html +++ b/tests/data/html/image-mime_type.html @@ -1 +1 @@ -
    python
    +
    python
    \ No newline at end of file diff --git a/tests/data/html/image-missing_caption.html b/tests/data/html/image-missing_caption.html index 17ff431..07a2506 100644 --- a/tests/data/html/image-missing_caption.html +++ b/tests/data/html/image-missing_caption.html @@ -1 +1 @@ -
    Sleepy Kitten
    +
    Sleepy Kitten
    \ No newline at end of file diff --git a/tests/data/html/image-no_caption.html b/tests/data/html/image-no_caption.html index 17ff431..07a2506 100644 --- a/tests/data/html/image-no_caption.html +++ b/tests/data/html/image-no_caption.html @@ -1 +1 @@ -
    Sleepy Kitten
    +
    Sleepy Kitten
    \ No newline at end of file diff --git a/tests/data/html/image.html b/tests/data/html/image.html index 7bb07f8..f0adfa1 100644 --- a/tests/data/html/image.html +++ b/tests/data/html/image.html @@ -1 +1 @@ -
    Sleepy Kitten
    <script>alert(23)</script>Cute Kitty
    +
    Sleepy Kitten
    <script>alert(23)</script>Cute Kitty
    \ No newline at end of file diff --git a/tests/data/html/ordered_list.html b/tests/data/html/ordered_list.html index 0549f97..af4a6d7 100644 --- a/tests/data/html/ordered_list.html +++ b/tests/data/html/ordered_list.html @@ -1 +1 @@ -

    Scroll

    1. One

    2. Two

    3. Three

    1. One

    2. Two

    3. Three

    +

    Scroll

    1. One

    2. Two

    3. Three

    1. One

    2. Two

    3. Three

    \ No newline at end of file diff --git a/tests/data/json/image.json b/tests/data/json/image.json index 55d981d..5720172 100644 --- a/tests/data/json/image.json +++ b/tests/data/json/image.json @@ -4,10 +4,13 @@ { "type": "image", "attrs": { - "src": { "image": "https://placekitten.com/200/301", "fallback": "https://placekitten.com/198/654" }, + "src": { + "image": "https://placekitten.com/200/301?text=\"escape me\"", + "fallback": "https://placekitten.com/198/654" + }, "alt": "Sleepy Kitten", "caption": "Cute Kitty" } } - ] + ] } diff --git a/tests/test_transform.py b/tests/test_transform.py index bccf3a3..75c3822 100644 --- a/tests/test_transform.py +++ b/tests/test_transform.py @@ -1,7 +1,8 @@ import os + import pytest -import tiptapy +import tiptapy tags_to_test = ( "simple", @@ -41,7 +42,7 @@ "document-is_renderable", "document-sketch", "camel-case", - "data_attributes" + "data_attributes", ) @@ -49,22 +50,31 @@ class config: """ Config class to store constans which are used by the othe nodes. """ + DOMAIN = "python.org" -def build_test_data(): +def build_test_data(rebuild_html=False): """ Scan data directories and return test data + + :param rebuild_html: If True, rebuild the html files """ - store = {'json': {}, 'html': {}} + store = {"json": {}, "html": {}} for data_type in store: - dir_path = os.path.abspath(f'tests/data/{data_type}/') + dir_path = os.path.abspath(f"tests/data/{data_type}/") for file in os.listdir(dir_path): file_path = os.path.join(dir_path, file) with open(file_path) as f: data = f.read() - store[data_type][file.split(f'.{data_type}')[0]] = data - return store['json'], store['html'] + store[data_type][file.split(f".{data_type}")[0]] = data + + # Use this to (re)generate the html files + if rebuild_html and data_type == "json": + with open(file_path.replace("json", "html"), "w") as f: + f.write(tiptapy.BaseDoc(config).render(data)) + + return store["json"], store["html"] json_data, html_data = build_test_data() diff --git a/tiptapy/__init__.py b/tiptapy/__init__.py index 4562159..d3147fd 100644 --- a/tiptapy/__init__.py +++ b/tiptapy/__init__.py @@ -1,31 +1,35 @@ +import json import os import sys -import json from html import escape -from jinja2 import FileSystemLoader, Environment, select_autoescape from typing import Dict -from .image import url2mime -from .macros import (make_img_src, build_link_handler, - get_audio_player_block, get_doc_block) +from jinja2 import Environment, FileSystemLoader, select_autoescape + +from .image import url2mime +from .macros import ( + build_link_handler, + get_audio_player_block, + get_doc_block, + make_img_src, +) -__version__ = '0.17.0' +__version__ = "0.18.0" renderers: Dict = {} def init_env(path, config): - env = Environment(loader=FileSystemLoader(path), - autoescape=select_autoescape( - enabled_extensions=('html'))) + env = Environment( + loader=FileSystemLoader(path), + autoescape=select_autoescape(enabled_extensions=("html")), + ) # https://stackoverflow.com/a/6038550 - env.globals['url2mime'] = url2mime - env.globals['make_img_src'] = make_img_src - env.globals['handle_links'] = build_link_handler(config) - # Cause jinja2 `e` filter is not exactly same as html.escape - env.globals['escape'] = escape - env.globals['get_audio_player_block'] = get_audio_player_block - env.globals['get_doc_block'] = get_doc_block + env.globals["url2mime"] = url2mime + env.globals["make_img_src"] = make_img_src + env.globals["handle_links"] = build_link_handler(config) + env.globals["get_audio_player_block"] = get_audio_player_block + env.globals["get_doc_block"] = get_doc_block return env @@ -39,11 +43,26 @@ def _get_abs_template_path(path_str): return os.path.join(pkg_dir, path_str) -class BaseDoc: +def escape_values_recursive(node): + skip_key = "html" # Skip escaping html values in embeds + if isinstance(node, dict): + for k, v in node.items(): + if k != skip_key: + node[k] = escape_values_recursive(v) + elif isinstance(node, list): + for i, v in enumerate(node): + node[i] = escape_values_recursive(v) + elif isinstance(node, str): + return escape(node) + return node - doc_type = 'doc' - templates_path = (_get_abs_template_path('templates'), - _get_abs_template_path('templates/extras')) + +class BaseDoc: + doc_type = "doc" + templates_path = ( + _get_abs_template_path("templates"), + _get_abs_template_path("templates/extras"), + ) locked = False # `locked` helps in templates determine to show/hide in anonymous views # Useful in case where code is referring same template for both protected @@ -51,10 +70,11 @@ class BaseDoc: def __init__(self, config): environ = init_env(self.templates_path, config) - self.t = environ.get_template(f'{self.doc_type}.html') - self.t.environment.globals['locked'] = self.locked + self.t = environ.get_template(f"{self.doc_type}.html") + self.t.environment.globals["locked"] = self.locked def render(self, in_data): in_data = in_data if isinstance(in_data, dict) else json.loads(in_data) node = in_data if isinstance(in_data, dict) else json.loads(in_data) + node = escape_values_recursive(node) return self.t.render(node=node) diff --git a/tiptapy/image.py b/tiptapy/image.py index 324ba3f..01aaa6b 100644 --- a/tiptapy/image.py +++ b/tiptapy/image.py @@ -3,24 +3,24 @@ # Link: https://developer.mozilla.org/en-US/docs/Web/Media/Formats/Image_types from os.path import splitext -class SupportedFormatsMapper(dict): +class SupportedFormatsMapper(dict): def __missing__(self, ext): - return 'image' + return "image" SUPPORTED_FORMATS_MAP = SupportedFormatsMapper( - PNG='image/png', - JPG='image/jpeg', - JPEG='image/jpeg', - GIF='image/gif', - BMP='image/bmp', - WEBP='image/webp', - SVG='image/svg+xml' + PNG="image/png", + JPG="image/jpeg", + JPEG="image/jpeg", + GIF="image/gif", + BMP="image/bmp", + WEBP="image/webp", + SVG="image/svg+xml", ) def url2mime(url): ext = splitext(url)[-1] - ext = (ext[1:] if ext.startswith('.') else ext).upper() + ext = (ext[1:] if ext.startswith(".") else ext).upper() return SUPPORTED_FORMATS_MAP[ext] diff --git a/tiptapy/macros.py b/tiptapy/macros.py index d0043d6..b5b4b82 100644 --- a/tiptapy/macros.py +++ b/tiptapy/macros.py @@ -1,37 +1,37 @@ import pkgutil -from html import escape -from urllib.parse import urlparse from string import Template +from urllib.parse import urlparse def make_img_src(attrs): - alt = attrs.get('alt', '').strip() - height = attrs.get('height', '') - width = attrs.get('width', '') - fallback_url = attrs['src']['fallback'] - image_src = f'img src="{fallback_url}"' + alt = attrs.get("alt", "").strip() + height = attrs.get("height", "") + width = attrs.get("width", "") + fallback_url = attrs["src"]["fallback"].strip() + img = f'img src="{fallback_url}"' if alt: - image_src += f' alt="{escape(alt)}"' - if height and width: - image_src += f'width="{width}" height="{height}"' + img += f' alt="{alt}"' + if width: + img += f' width="{width}"' + if height: + img += f' height="{height}"' - return image_src + return img def build_link_handler(config): - def handle_links(attrs): - retval = None if attrs: - url = attrs.get("href") or "" + url = attrs.get("href", "").strip() link = urlparse(url) - if not link.netloc.endswith(config.DOMAIN): + if not ( + link.netloc == config.DOMAIN + or link.netloc.endswith(f".{config.DOMAIN}") + ): attrs["target"] = "_blank" attrs["rel"] = "noopener nofollow" - retval = " ".join( - f'{k}="{escape(v)}"' for k, v in attrs.items() if v is not None - ) + retval = " ".join(f'{k}="{v}"' for k, v in attrs.items() if v is not None) return retval return handle_links @@ -39,13 +39,17 @@ def handle_links(attrs): def get_audio_player_block(): audio_player_block = pkgutil.get_data( - __name__, 'templates/stack-audio-player.html').decode() + __name__, "templates/stack-audio-player.html" + ).decode() return audio_player_block def get_doc_block(ext, fname, size, src): - document_block = pkgutil.get_data(__name__, 'templates/stack-document.html').decode() + document_block = pkgutil.get_data( + __name__, "templates/stack-document.html" + ).decode() document = Template(document_block) - html = document.substitute(fileformat=ext[:4], filename=fname, - filesize=size, filesrc=src) + html = document.substitute( + fileformat=ext[:4], filename=fname, filesize=size, filesrc=src + ) return html diff --git a/tiptapy/templates/codeBlock.html b/tiptapy/templates/codeBlock.html index b0c2886..262db35 100644 --- a/tiptapy/templates/codeBlock.html +++ b/tiptapy/templates/codeBlock.html @@ -1,6 +1,6 @@ {%- set language = node.attrs.language|default("") -%} {%- if node.content -%} - {%- set text = escape(node.content[0].text) -%} + {%- set text = node.content[0].text -%} {%- if text -%}
           {%- if language -%}
    diff --git a/tiptapy/templates/code_block.html b/tiptapy/templates/code_block.html
    index b0c2886..262db35 100644
    --- a/tiptapy/templates/code_block.html
    +++ b/tiptapy/templates/code_block.html
    @@ -1,6 +1,6 @@
     {%- set language = node.attrs.language|default("") -%}
     {%- if node.content -%}
    -  {%- set text = escape(node.content[0].text) -%}
    +  {%- set text = node.content[0].text -%}
       {%- if text -%}
         
           {%- if language -%}
    diff --git a/tiptapy/templates/doc.html b/tiptapy/templates/doc.html
    index 518e72e..f74b827 100644
    --- a/tiptapy/templates/doc.html
    +++ b/tiptapy/templates/doc.html
    @@ -2,10 +2,10 @@
       
     {%- endif -%}
     {%- for item in node.content -%}
    -  {%- with template=item.type + '.html', node=item -%} 
    +  {%- with template=item.type + '.html', node=item -%}
         {%- include template -%}
       {%- endwith -%}
     {%- endfor -%}
     {%- if node.attrs and node.attrs.data -%}
       
    -{%- endif -%} \ No newline at end of file +{%- endif -%} diff --git a/tiptapy/templates/embed.html b/tiptapy/templates/embed.html index 21404fc..05a0df0 100644 --- a/tiptapy/templates/embed.html +++ b/tiptapy/templates/embed.html @@ -9,7 +9,7 @@ {%- endfor -%} {%- if node.attrs.type == "video" -%} {%- if node.attrs.caption and node.attrs.caption|trim -%} -
    {{node.attrs.html}}
    {{escape(node.attrs.caption)}}
    +
    {{node.attrs.html}}
    {{node.attrs.caption}}
    {%- else -%}
    {{html}}
    {%- endif -%} diff --git a/tiptapy/templates/extras/audio.html b/tiptapy/templates/extras/audio.html index db31a4c..787e7c0 100644 --- a/tiptapy/templates/extras/audio.html +++ b/tiptapy/templates/extras/audio.html @@ -1,12 +1,13 @@ {%- if node.attrs.src -%} {%- set caption = node.attrs.caption|trim -%} {%- set audio_player_block = get_audio_player_block() -%} + {%- set src = node.attrs.src|trim -%}
    {%- if caption -%} -
    {{audio_player_block}}
    {{escape(caption)}}
    +
    {{audio_player_block}}
    {{caption}}
    {%- else -%} -
    {{audio_player_block}}
    +
    {{audio_player_block}}
    {%- endif -%}
    {%- endif -%} diff --git a/tiptapy/templates/extras/document.html b/tiptapy/templates/extras/document.html index 03091c3..c5fdeb9 100644 --- a/tiptapy/templates/extras/document.html +++ b/tiptapy/templates/extras/document.html @@ -7,7 +7,7 @@ {%- set doc_block = get_doc_block(ext, fname, size, src) -%}
    {%- if caption -%} - {{doc_block}}
    {{escape(caption)}}
    + {{doc_block}}
    {{caption}}
    {%- else -%} {{doc_block}} {%- endif -%} diff --git a/tiptapy/templates/extras/featuredimage.html b/tiptapy/templates/extras/featuredimage.html index a1b06a7..5e8119f 100644 --- a/tiptapy/templates/extras/featuredimage.html +++ b/tiptapy/templates/extras/featuredimage.html @@ -1,7 +1,7 @@ {%- if node.attrs.src -%} - {%- set image_url = node.attrs.src.image -%} + {%- set image_url = node.attrs.src.image|trim -%} {%- set image_type = url2mime(image_url) -%} - {%- set fallback_url = node.attrs['src']['fallback'] -%} + {%- set fallback_url = node.attrs.src.fallback|trim -%} {%- set fallback_type = url2mime(fallback_url) -%} {%- set caption = node.attrs.caption|trim -%} @@ -12,7 +12,7 @@ {%- if image_url or fallback_url -%} {%- endif -%} diff --git a/tiptapy/templates/image.html b/tiptapy/templates/image.html index 8d3dc83..ac0f7d7 100644 --- a/tiptapy/templates/image.html +++ b/tiptapy/templates/image.html @@ -3,15 +3,15 @@ {%- set alt = node.attrs.alt|trim -%} {%- set height = node.attrs.height -%} {%- set width = node.attrs.width -%} - {%- set image_url = node.attrs.src.image -%} + {%- set image_url = node.attrs.src.image|trim -%} {%- set image_type = url2mime(image_url) -%} - {%- set fallback_url = node.attrs['src']['fallback'] -%} + {%- set fallback_url = node.attrs.src.fallback|trim -%} {%- set fallback_type = url2mime(fallback_url) -%} {%- if image_url or fallback_url -%}
    <{{ make_img_src(node.attrs) }}/> {%- if caption -%} -
    {{ escape(caption) }}
    +
    {{ caption }}
    {%- endif -%}
    {%- endif -%} diff --git a/tiptapy/templates/text.html b/tiptapy/templates/text.html index d862f37..93d0d35 100644 --- a/tiptapy/templates/text.html +++ b/tiptapy/templates/text.html @@ -1,4 +1,4 @@ -{% set ns = namespace(text=escape(node.text)) %} +{% set ns = namespace(text=node.text) %} {%- for mark in node.marks -%} {%- set ns.text -%} {%- with template='marks/' + mark.type + '.html', text=ns.text, mark=mark -%} @@ -7,4 +7,4 @@ {%- endset -%} {%- endfor -%} -{{ns.text}} \ No newline at end of file +{{ns.text}}