0

What I'm trying to achieve is to upload a Confluence page content that contains code examples, and I'd like these code examples to use the {code} macro plugin that provides syntax highlighting when viewing the page.

I've found that the code macro stores 2 formats on Confluence, respectively for body.storage and body.view:

<ac:structured-macro ac:name="code" ac:schema-version="1" ac:macro- id="37fecf11-d435-452a-90c7-da19f3821b4c">
  <ac:parameter ac:name="language">bash</ac:parameter>
  <ac:parameter ac:name="linenumbers">true</ac:parameter>
  <ac:plain-text-body><![CDATA[ // code goes here ]]></ac:plain-text-body>
</ac:structured-macro>

and

<div class="code panel pdl" style="border-width: 1px;">
   <div class="codeContent panelContent pdl">
     <pre class="syntaxhighlighter-pre">
     // code goes here
     </pre>
   </div>
</div>

I've tried uploading both to Confluence using the API, but each time, the code block is rendered as a simple <pre/> element, and syntax highlighting is not rendered.

Any help appreciated.

Note: This is how I update the content through the API: https://docs.atlassian.com/atlassian-confluence/REST/latest-server/#content-update Typically:

HTML = '<div class="code panel pdl" style="border-width: 1px;">
   <div class="codeContent panelContent pdl">
     <pre class="syntaxhighlighter-pre">
     $ DIST=`cat /etc/*release`
     $ echo $DIST
     </pre>
   </div>
</div>'

data = json.dumps(
    {
        'id': '%d' % PAGEID,
        'type': 'page',
        'title': TITLE,
        'space': {
            'key': SPACE
        },
        'version': {
            'number': VERSION +1
        },
        'body': {
            'storage': {
                'representation': 'storage',
                'value': HTML
            }
        }
    }   
)
rPut = requests.put(
    url,
    data = data,
    auth = (USER, PWD),
    headers = {
        'Content-Type': 'application/json',
        'Accept': 'application/json'
    }
)

Any help appreciated,

Guillaume S.
  • 272
  • 1
  • 3
  • 13
  • Please add an example of how you send the API call, both request URL and request body (even if you test the API with the REST API browser or some other tool like curl). I'm pretty sure, you need to specify the code in the `body.storage` format to use Confluence macros in the resulting content. – Lukas Körfer Mar 06 '17 at 09:15
  • Just added an example of how I send the request with Python. But basically, whether I send the "HTML" example, or the one with the macros listed above "ac:xxxx", it doesn't change a thing and the code snippet is still not syntax-highlighted. – Guillaume S. Mar 06 '17 at 12:28
  • But your example uses HTML as `body`.`value`, while you say the representation is `storage`. You can try to copy a content by first reading the content in `body.storage` format and then creating another content with the response of the read request. – Lukas Körfer Mar 06 '17 at 14:10
  • 2
    I see your representation is storage (`representation : storage`) but your sending HTML. I think the value should be in Confluence storage format (the Confluence XML parser will accept properly formed HTML). Have you tried replacing the HTML value with `...`? – jpllosa Mar 06 '17 at 14:58
  • As @jpllosa said, if you send a storage representation, you should send your HTML block with the `ac:structured-macro` block. – mtheriault Mar 07 '17 at 12:23
  • Yes, I have tried to push the macro format, with `...`, as listed above, but it doesn't change a thing and the syntax highlighting still does not work. – Guillaume S. Mar 08 '17 at 09:52
  • OK, back to this: when I push a block like ``... using `body.storage.representation=storage`, I get a non-formatted result in the resulting page! When I pull the page in storage representation again, all tags are gone which surrounded the code excerpt, it's all just straight text, including the value of the true, etc. – Guillaume S. Mar 29 '17 at 14:50
  • Did you try to omit ```ac:macro- id``` attribute? [Here](https://community.atlassian.com/t5/Confluence-questions/What-is-the-purpose-of-ac-macro-id-Can-it-be-omitted-from-User/qaq-p/799325) people recommend to not specify it. – Naeel Maqsudov Sep 10 '18 at 08:57
  • And, BTW, I beleave the space character within the ```ac:macro- id``` is just a misprint in your example. – Naeel Maqsudov Sep 10 '18 at 09:00

2 Answers2

0

when I get content in storage format, a syntax-highlighted code block, looks like the below

<div class="code panel pdl conf-macro output-block" data-hasbody="true" data-macro-name="code" style="border-width: 1px;">
    <div class="codeHeader panelHeader pdl" style="border-bottom-width: 1px;">
        <b>SANITIZED CODE HEADER</b>
    </div>
    <div class="codeContent panelContent pdl">
        <pre class="syntaxhighlighter-pre" data-syntaxhighlighter-params="brush: bash; gutter: false; first-line: 1; theme: DJango" data-theme="DJango">
        SANITIZED CODE CONTENT
        </pre>
    </div>
</div>
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 16 '22 at 15:00
0

You can use the Code Macro in storage format as follows:

<ac:structured-macro ac:name="code">
  <ac:parameter ac:name="title">This is my title</ac:parameter>
  <ac:parameter ac:name="theme">FadeToGrey</ac:parameter>
  <ac:parameter ac:name="linenumbers">true</ac:parameter>
  <ac:parameter ac:name="language">xml</ac:parameter>
  <ac:parameter ac:name="firstline">0001</ac:parameter>
  <ac:parameter ac:name="collapse">true</ac:parameter>
  <ac:plain-text-body><![CDATA[<b>This is my code</b>]]></ac:plain-text-body>
</ac:structured-macro>

Ref: https://www.cwiki.us/display/CONF54/Confluence+Storage+Format+for+Macros#ConfluenceStorageFormatforMacros-CodeBlockmacro