Changes for page Export To Markdown

Last modified by Tobias Wintrich on 2026/03/27 13:39

From version 4.1
edited by Tobias Wintrich
on 2026/03/27 13:23
Change comment: There is no comment for this version
To version 7.1
edited by Tobias Wintrich
on 2026/03/27 13:39
Change comment: There is no comment for this version

Summary

Details

Page properties
Content
... ... @@ -1,33 +1,66 @@
1 1  {{groovy}}
2 -import org.xwiki.model.reference.*
2 +import org.xwiki.model.reference.EntityReferenceSerializer
3 3  import java.io.File
4 4  
5 -if (request.confirm == '1') {
6 - // Festes Export-Verzeichnis
7 - def tmpDir = new File("/usr/local/xwiki/data/md-export")
8 - tmpDir.mkdirs()
5 +if (request.get('confirm') == '1') {
9 9  
10 - services.query.xwql(
11 - "select distinct doc.fullName from Document doc " +
12 - "where doc.space like 'HowTos' or doc.space like 'HowTos.%'"
13 - ).execute().each() {
7 + // Zielverzeichnis
8 + def exportDir = new File("/usr/local/xwiki/data/md-export")
9 + exportDir.mkdirs()
14 14  
15 - print "* Converting ${it} to MD..."
11 + // Serializer für Ordnerstruktur
12 + def pathSerializer = services.component.getInstance(
13 + EntityReferenceSerializer.TYPE_STRING, "fspath"
14 + )
16 16  
17 - def itemDoc = xwiki.getDocument(it)
18 - def newContent = services.rendering.render(itemDoc.getXDOM(), 'markdown/1.2')
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"
19 19  
20 - def pathSerializer = services.component.getInstance(
21 - EntityReferenceSerializer.TYPE_STRING, 'fspath'
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 + // Fix für XWiki Image Syntax ![[file|text]]
37 + markdown = markdown.replaceAll(
38 + /!\[\[([^|\]]+)\|([^\]]+)\]\]/,
39 + '![\$2](\$1)'
22 22   )
23 23  
24 - def outputFile = new File(tmpDir, pathSerializer.serialize(itemDoc.documentReference))
42 + // Dateipfad erzeugen
43 + def relativePath = pathSerializer.serialize(doc.documentReference)
44 + def outputFile = new File(exportDir, relativePath + ".md")
45 +
25 25   outputFile.parentFile.mkdirs()
26 - outputFile.text = newContent
47 + outputFile.write(markdown, "UTF-8")
27 27  
28 - println "Saved in ${outputFile.toString()}"
49 + println(" -> Saved page to " + outputFile)
50 +
51 + // Anhänge exportieren
52 + for (attachment in doc.getAttachmentList()) {
53 +
54 + def attachmentFile = new File(outputFile.parentFile, attachment.getFilename())
55 +
56 + attachmentFile.withOutputStream { os ->
57 + os << attachment.getContentInputStream()
58 + }
59 +
60 + println(" -> Exported attachment " + attachment.getFilename())
61 + }
29 29   }
30 30  }
31 31  
32 -println "[[Export>>||queryString='confirm=1']]"
65 +println("[[Export starten>>||queryString='confirm=1']]")
33 33  {{/groovy}}