How to export page to XWiki/2.1

Hi!

I need to export source code of page.

I found discuss

Where there is example of groovy script to export page to markdown
https://snippets.xwiki.org/xwiki/bin/view/Extension/Export%20pages%20to%20Markdown/

But when I change in this script

'markdown/1.2'

to xwiki/2.1
script create empty files with ‘null’ inside

Could any one tell how I need to change script to download source code as is.
All my pages are in Xwiki/2.1 syntax.

As is but with rendered macros on pages.

So if your pages are already in xwiki/2.1 syntax this script is really not what you need since the main point of this script is to do syntax conversion. That means that itemDoc.content is enough to get the content of the page in syntax xwiki/2.1.

It’s not very clear to me what you want to do with it, as it might have an impact on how to export the content. For example, if your pages contains xwiki/2.1 content then you could simply use the XAR export which simply produce a zip file containing an XML for each page with the content in it.

1 Like

Hello!

I have the following use case:

  1. User selects pages to export within XWiki.
  2. User presses a button to generate a DOCX with custom template (need to apply custom styles).
  3. XWiki exports selected pages in xwiki/2.1 syntax
  4. System passes pages to pandoc.
  5. Pandoc creates a DOCX file using custom LUA filter and template from user.

For first step I need to create zip with selected pages in xwiki/2.1 syntax.
And I XML format is not suitable for me;

This variant is working for me

{{groovy}}
import org.xwiki.environment.*
import org.xwiki.model.reference.*

if (request.confirm == '1') {
  services.query.xwql("select distinct doc.fullName from Document doc where doc.space like 'Sandbox' or doc.space like 'Sandbox.%'").execute().each() {
    print "* Converting ${it} to MD..."
    def itemDoc = xwiki.getDocument(it)
    def newContent = itemDoc.getContent()
    def tmpDir = new File(services.component.getInstance(Environment.class).temporaryDirectory, 'md-export')
    def pathSerializer = services.component.getInstance(EntityReferenceSerializer.TYPE_STRING, 'fspath')
    def outputFile = new File(tmpDir, pathSerializer.serialize(itemDoc.documentReference))
    outputFile.parentFile.mkdirs()
    outputFile << newContent
    println "Saved in ${outputFile.toString()}"
  }
}

println "[[Export>>||queryString='confirm=1']]"
{{/groovy}}

Glad you found a solution for your use case :slight_smile:

1 Like