Wiki-Quellcode von Export To Markdown
Zuletzt geändert von Tobias Wintrich am 2026/03/27 14:02
Zeige letzte Bearbeiter
| author | version | line-number | content |
|---|---|---|---|
| 1 | {{groovy}} | ||
| 2 | import org.xwiki.model.reference.EntityReferenceSerializer | ||
| 3 | import java.io.File | ||
| 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 | def query = "select doc.fullName from Document doc " + | ||
| 17 | "where (" + | ||
| 18 | "doc.space like 'HowTos' or doc.space like 'HowTos.%' or " + | ||
| 19 | "doc.space like 'Handbuecher' or doc.space like 'Handbuecher.%' or " + | ||
| 20 | "doc.space like 'Hardware' or doc.space like 'Hardware.%'" + | ||
| 21 | ") and doc.hidden = false" | ||
| 22 | |||
| 23 | def results = services.query.xwql(query).execute() | ||
| 24 | |||
| 25 | for (fullName in results) { | ||
| 26 | |||
| 27 | println("* Exporting " + fullName) | ||
| 28 | |||
| 29 | def doc = xwiki.getDocument(fullName) | ||
| 30 | |||
| 31 | if (doc.isHidden()) { | ||
| 32 | continue | ||
| 33 | } | ||
| 34 | |||
| 35 | // Markdown erzeugen | ||
| 36 | def markdown = services.rendering.render(doc.getXDOM(), "markdown/1.2") | ||
| 37 | |||
| 38 | // Fix für XWiki Image Syntax ![[file|text]] | ||
| 39 | markdown = markdown.replaceAll( | ||
| 40 | /!\[\[([^|\]]+)\|([^\]]+)\]\]/, | ||
| 41 | '' | ||
| 42 | ) | ||
| 43 | |||
| 44 | // Dateipfad erzeugen | ||
| 45 | def relativePath = pathSerializer.serialize(doc.documentReference) | ||
| 46 | def outputFile = new File(exportDir, relativePath + ".md") | ||
| 47 | |||
| 48 | outputFile.parentFile.mkdirs() | ||
| 49 | outputFile.write(markdown, "UTF-8") | ||
| 50 | |||
| 51 | println(" -> Saved page to " + outputFile) | ||
| 52 | |||
| 53 | // Anhänge exportieren | ||
| 54 | for (attachment in doc.getAttachmentList()) { | ||
| 55 | |||
| 56 | def attachmentFile = new File(outputFile.parentFile, attachment.getFilename()) | ||
| 57 | |||
| 58 | attachmentFile.withOutputStream { os -> | ||
| 59 | os << attachment.getContentInputStream() | ||
| 60 | } | ||
| 61 | |||
| 62 | println(" -> Exported attachment " + attachment.getFilename()) | ||
| 63 | } | ||
| 64 | } | ||
| 65 | } | ||
| 66 | |||
| 67 | println("[[Export starten>>||queryString='confirm=1']]") | ||
| 68 | {{/groovy}} |