Wiki-Quellcode von Export To Markdown
Zuletzt geändert von Tobias Wintrich am 2026/03/27 14:02
Verstecke letzte Bearbeiter
| author | version | line-number | content |
|---|---|---|---|
| |
2.1 | 1 | {{groovy}} |
| |
6.1 | 2 | import org.xwiki.model.reference.EntityReferenceSerializer |
| |
4.1 | 3 | import java.io.File |
| |
1.1 | 4 | |
| |
6.1 | 5 | if (request.get('confirm') == '1') { |
| |
4.1 | 6 | |
| |
5.1 | 7 | // Zielverzeichnis |
| 8 | def exportDir = new File("/usr/local/xwiki/data/md-export") | ||
| 9 | exportDir.mkdirs() | ||
| |
4.1 | 10 | |
| |
6.1 | 11 | // Serializer für Ordnerstruktur |
| |
5.1 | 12 | def pathSerializer = services.component.getInstance( |
| |
6.1 | 13 | EntityReferenceSerializer.TYPE_STRING, "fspath" |
| |
5.1 | 14 | ) |
| |
4.1 | 15 | |
| |
7.1 | 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" | ||
| |
4.1 | 22 | |
| |
6.1 | 23 | def results = services.query.xwql(query).execute() |
| |
4.1 | 24 | |
| |
6.1 | 25 | for (fullName in results) { |
| |
5.1 | 26 | |
| |
6.1 | 27 | println("* Exporting " + fullName) |
| 28 | |||
| |
5.1 | 29 | def doc = xwiki.getDocument(fullName) |
| 30 | |||
| 31 | if (doc.isHidden()) { | ||
| |
6.1 | 32 | continue |
| |
5.1 | 33 | } |
| 34 | |||
| |
6.1 | 35 | // Markdown erzeugen |
| 36 | def markdown = services.rendering.render(doc.getXDOM(), "markdown/1.2") | ||
| |
7.1 | 37 | |
| 38 | // Fix für XWiki Image Syntax ![[file|text]] | ||
| 39 | markdown = markdown.replaceAll( | ||
| 40 | /!\[\[([^|\]]+)\|([^\]]+)\]\]/, | ||
| 41 | '' | ||
| 42 | ) | ||
| |
5.1 | 43 | |
| |
6.1 | 44 | // Dateipfad erzeugen |
| |
5.1 | 45 | def relativePath = pathSerializer.serialize(doc.documentReference) |
| 46 | def outputFile = new File(exportDir, relativePath + ".md") | ||
| 47 | |||
| |
1.1 | 48 | outputFile.parentFile.mkdirs() |
| |
6.1 | 49 | outputFile.write(markdown, "UTF-8") |
| |
4.1 | 50 | |
| |
6.1 | 51 | println(" -> Saved page to " + outputFile) |
| |
5.1 | 52 | |
| 53 | // Anhänge exportieren | ||
| |
6.1 | 54 | for (attachment in doc.getAttachmentList()) { |
| |
5.1 | 55 | |
| |
6.1 | 56 | def attachmentFile = new File(outputFile.parentFile, attachment.getFilename()) |
| |
5.1 | 57 | |
| 58 | attachmentFile.withOutputStream { os -> | ||
| |
6.1 | 59 | os << attachment.getContentInputStream() |
| |
5.1 | 60 | } |
| 61 | |||
| |
6.1 | 62 | println(" -> Exported attachment " + attachment.getFilename()) |
| |
5.1 | 63 | } |
| |
1.1 | 64 | } |
| 65 | } | ||
| 66 | |||
| |
6.1 | 67 | println("[[Export starten>>||queryString='confirm=1']]") |
| |
2.1 | 68 | {{/groovy}} |