Changes for page Export To Markdown
Last modified by Tobias Wintrich on 2026/03/27 13:39
From version 6.1
edited by Tobias Wintrich
on 2026/03/27 13:30
on 2026/03/27 13:30
Change comment:
There is no comment for this version
To version 3.1
edited by René Vögeli
on 2026/03/25 10:32
on 2026/03/25 10:32
Change comment:
There is no comment for this version
Summary
-
Page properties (2 modified, 0 added, 0 removed)
Details
- Page properties
-
- Author
-
... ... @@ -1,1 +1,1 @@ 1 -XWiki. twintrich1 +XWiki.rvoegeli - Content
-
... ... @@ -1,60 +1,20 @@ 1 1 {{groovy}} 2 -import org.xwiki. model.reference.EntityReferenceSerializer3 -import java.io.File2 +import org.xwiki.environment.* 3 +import org.xwiki.model.reference.* 4 4 5 -if (request.get('confirm') == '1') { 6 - 7 - // Zielverzeichnis 8 - def exportDir = new File("/usr/local/xwiki/data/md-export") 9 - exportDir.mkdirs() 10 - 11 - // Serializer für Ordnerstruktur 12 - def pathSerializer = services.component.getInstance( 13 - EntityReferenceSerializer.TYPE_STRING, "fspath" 14 - ) 15 - 16 - // Query (einzeilig!) 17 - def query = "select doc.fullName from Document doc " + 18 - "where (doc.space like 'HowTos' or doc.space like 'HowTos.%') " + 19 - "and doc.hidden = false" 20 - 21 - def results = services.query.xwql(query).execute() 22 - 23 - for (fullName in results) { 24 - 25 - println("* Exporting " + fullName) 26 - 27 - def doc = xwiki.getDocument(fullName) 28 - 29 - if (doc.isHidden()) { 30 - continue 31 - } 32 - 33 - // Markdown erzeugen 34 - def markdown = services.rendering.render(doc.getXDOM(), "markdown/1.2") 35 - 36 - // Dateipfad erzeugen 37 - def relativePath = pathSerializer.serialize(doc.documentReference) 38 - def outputFile = new File(exportDir, relativePath + ".md") 39 - 5 +if (request.confirm == '1') { 6 + services.query.xwql("select distinct doc.fullName from Document doc where doc.space like 'HowTos' or doc.space like 'HowTos.%'").execute().each() { 7 + print "* Converting ${it} to MD..." 8 + def itemDoc = xwiki.getDocument(it) 9 + def newContent = services.rendering.render(itemDoc.getXDOM(), 'markdown/1.2') 10 + def tmpDir = new File(services.component.getInstance(Environment.class).temporaryDirectory, 'md-export') 11 + def pathSerializer = services.component.getInstance(EntityReferenceSerializer.TYPE_STRING, 'fspath') 12 + def outputFile = new File(tmpDir, pathSerializer.serialize(itemDoc.documentReference)) 40 40 outputFile.parentFile.mkdirs() 41 - outputFile.write(markdown, "UTF-8") 42 - 43 - println(" -> Saved page to " + outputFile) 44 - 45 - // Anhänge exportieren 46 - for (attachment in doc.getAttachmentList()) { 47 - 48 - def attachmentFile = new File(outputFile.parentFile, attachment.getFilename()) 49 - 50 - attachmentFile.withOutputStream { os -> 51 - os << attachment.getContentInputStream() 52 - } 53 - 54 - println(" -> Exported attachment " + attachment.getFilename()) 55 - } 14 + outputFile << newContent 15 + println "Saved in ${outputFile.toString()}" 56 56 } 57 57 } 58 58 59 -println ("[[Exportstarten>>||queryString='confirm=1']]")19 +println "[[Export>>||queryString='confirm=1']]" 60 60 {{/groovy}}