Wiki source code of Export To Markdown
Last modified by Tobias Wintrich on 2026/03/27 13:39
Hide last authors
| 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 | |
| |
6.1 | 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" | ||
| |
4.1 | 20 | |
| |
6.1 | 21 | def results = services.query.xwql(query).execute() |
| |
4.1 | 22 | |
| |
6.1 | 23 | for (fullName in results) { |
| |
5.1 | 24 | |
| |
6.1 | 25 | println("* Exporting " + fullName) |
| 26 | |||
| |
5.1 | 27 | def doc = xwiki.getDocument(fullName) |
| 28 | |||
| 29 | if (doc.isHidden()) { | ||
| |
6.1 | 30 | continue |
| |
5.1 | 31 | } |
| 32 | |||
| |
6.1 | 33 | // Markdown erzeugen |
| 34 | def markdown = services.rendering.render(doc.getXDOM(), "markdown/1.2") | ||
| |
7.1 | 35 | |
| 36 | // Fix für XWiki Image Syntax ![[file|text]] | ||
| 37 | markdown = markdown.replaceAll( | ||
| 38 | /!\[\[([^|\]]+)\|([^\]]+)\]\]/, | ||
| 39 | '' | ||
| 40 | ) | ||
| |
5.1 | 41 | |
| |
6.1 | 42 | // Dateipfad erzeugen |
| |
5.1 | 43 | def relativePath = pathSerializer.serialize(doc.documentReference) |
| 44 | def outputFile = new File(exportDir, relativePath + ".md") | ||
| 45 | |||
| |
1.1 | 46 | outputFile.parentFile.mkdirs() |
| |
6.1 | 47 | outputFile.write(markdown, "UTF-8") |
| |
4.1 | 48 | |
| |
6.1 | 49 | println(" -> Saved page to " + outputFile) |
| |
5.1 | 50 | |
| 51 | // Anhänge exportieren | ||
| |
6.1 | 52 | for (attachment in doc.getAttachmentList()) { |
| |
5.1 | 53 | |
| |
6.1 | 54 | def attachmentFile = new File(outputFile.parentFile, attachment.getFilename()) |
| |
5.1 | 55 | |
| 56 | attachmentFile.withOutputStream { os -> | ||
| |
6.1 | 57 | os << attachment.getContentInputStream() |
| |
5.1 | 58 | } |
| 59 | |||
| |
6.1 | 60 | println(" -> Exported attachment " + attachment.getFilename()) |
| |
5.1 | 61 | } |
| |
1.1 | 62 | } |
| 63 | } | ||
| 64 | |||
| |
6.1 | 65 | println("[[Export starten>>||queryString='confirm=1']]") |
| |
2.1 | 66 | {{/groovy}} |