CUE Print to Content Store

The template cue-print/cue-print-to-storyline/image converts a CUE Print image caption text to a pseudo-storyline version of a Content Store image story element.

The part of the template that is responsible for converting the caption looks like this:

{
    "name": "caption",
    "value": {{ caption.text_content|d("")|trim|tojson }},
    "annotations": []
}

The highlighted instruction in the value field extracts the text content (ignoring any markup), defaults to an empty string, trims any whitespace at the beginning and end of the text and then outputs the result as a JSON string.

In order to preserve any markup in the CUE Print text and allow corresponding storyline annotations to be created, the markup needs to be converted into pseudo-storyline operations. To achieve this, the value and annotations fields in the template need to be replaced with an ops field like this:

    "ops": [
{% with cutline = caption.content.data.ccitext.cutline.cutline_c.p|first %}
    {% for node in cutline recursive %}
        {% if node.is_text %}
            {
                "name": "",
                "text": {{ node.text_content|tojson }}
            }
        {% elif node.local_name == "bold" %}
            {
                "name": "bold",
                "sub": [
                    {{ loop(node) }}
                ]
            }
        {% endif %}
        {% if not loop.last %},{% endif %}
    {% endfor %}
{% endwith %}
    ]

The highlighted lines assign the content of the first paragraph in the CUE Print caption to a variable called cutline and pass it to the enclosed for loop. Any subsequent paragraphs (should they exist) are ignored. The long address of the caption paragraph reflects the deep XML structure used to represent captions in CUE Print:

<attribute group="ExtraInfo" kind="xml" name="CaptionTextAsXml">
  <content>
    <data format="text/xml">
       <cci:ccitext xmlns:cci="urn:schemas-ccieurope.com">
         ...
         <cci:cutline>
           <cci:cutline_c>
             <cci:p>
               Lorem ipsum <cci:bold>dolor sit amet</cci:bold>, consectetur adipiscing
               elit. Pellentesque quis lobortis ligula. Morbi hendrerit non purus sit
               amet volutpat. Mauris pulvinar velit ut augue vulputate, ac volutpat
               est molestie.
             </cci:p>
           </cci:cutline_c>
           ...
        </cci:cutline>
        ...
      </cci:ccitext>
    </data>
  </content>
</attribute>

The for loop recursively enumerates the contents of the paragraph:

{% for node in cutline recursive %}
    ...
{% endfor %}

The first if test selects every plain text node in the paragraph, and outputs the text as an operation with no name:

{% if node.is_text %}
    {
        "name": "",
        "text": {{ node.text_content|tojson }}
    }

The second test selects every <cci:bold> tag, wraps a bold operation its content, and recursively resubmits the content to the loop:

{% elif node.local_name == "bold" %}
    {
        "name": "bold",
        "sub": [
            {{ loop(node) }}
        ]
    }

The final else handles any other tags in the paragraph. It just recursively resubmits the content to the loop without wrapping an operation around it.

{% else %}
    {{ loop(node) }}
{% endif %}

The if at the end of the loop ensures that commas are inserted between the operations, as required by JSON syntax.

{% if not loop.last %},{% endif %}

As is the case with the Content Store – CUE Print template you can handle additional formats by adding more tests to the first if statement. For example:

{% elif node.local_name == "italic" %}
    {
        "name": "italic",
        "sub": [
            {{ loop(node) }}
        ]
    }

to convert italic tags to italic operations, or just:

{% elif node.local_name == "italic" %}

to strip out all italic content.