Wiki source code of Export To Markdown

Version 5.1 by Tobias Wintrich on 2026/03/27 13:28

Hide last authors
René Vögeli 2.1 1 {{groovy}}
René Vögeli 1.1 2 import org.xwiki.model.reference.*
Tobias Wintrich 5.1 3 import org.xwiki.model.EntityType
Tobias Wintrich 4.1 4 import java.io.File
René Vögeli 1.1 5
6 if (request.confirm == '1') {
Tobias Wintrich 4.1 7
Tobias Wintrich 5.1 8 // Zielverzeichnis
9 def exportDir = new File("/usr/local/xwiki/data/md-export")
10 exportDir.mkdirs()
Tobias Wintrich 4.1 11
Tobias Wintrich 5.1 12 // Serializer für saubere Ordnerstruktur
13 def pathSerializer = services.component.getInstance(
14 EntityReferenceSerializer.TYPE_STRING, 'fspath'
15 )
Tobias Wintrich 4.1 16
Tobias Wintrich 5.1 17 // Nur veröffentlichte Dokumente (hidden = false)
18 def query = """
19 select doc.fullName from Document doc
20 where (doc.space like 'HowTos' or doc.space like 'HowTos.%')
21 and doc.hidden = false
22 """
Tobias Wintrich 4.1 23
Tobias Wintrich 5.1 24 services.query.xwql(query).execute().each { fullName ->
Tobias Wintrich 4.1 25
Tobias Wintrich 5.1 26 println "* Exporting ${fullName}..."
27
28 def doc = xwiki.getDocument(fullName)
29
30 if (doc.isHidden()) {
31 return
32 }
33
34 // Markdown rendern
35 def markdown = services.rendering.render(doc.getXDOM(), 'markdown/1.2')
36
37 // Verzeichnisstruktur erzeugen
38 def relativePath = pathSerializer.serialize(doc.documentReference)
39 def outputFile = new File(exportDir, relativePath + ".md")
40
René Vögeli 1.1 41 outputFile.parentFile.mkdirs()
Tobias Wintrich 5.1 42 outputFile.text = markdown
Tobias Wintrich 4.1 43
Tobias Wintrich 5.1 44 println " -> Saved page to ${outputFile}"
45
46 // Anhänge exportieren
47 doc.attachmentList.each { attachment ->
48
49 def attachmentDir = outputFile.parentFile
50 def attachmentFile = new File(attachmentDir, attachment.filename)
51
52 attachmentFile.withOutputStream { os ->
53 os << attachment.contentInputStream
54 }
55
56 println " -> Exported attachment ${attachment.filename}"
57 }
René Vögeli 1.1 58 }
59 }
60
Tobias Wintrich 5.1 61 println "[[Export starten>>||queryString='confirm=1']]"
René Vögeli 2.1 62 {{/groovy}}