Wiki source code of Page Previewer
Last modified by Ondřej Steffl on 2020/05/03 23:46
Show last authors
author | version | line-number | content |
---|---|---|---|
1 | {{groovy}} | ||
2 | import groovy.json.JsonBuilder | ||
3 | import groovy.json.JsonOutput | ||
4 | |||
5 | import com.xpn.xwiki.api.Document | ||
6 | import com.xpn.xwiki.web.Utils | ||
7 | |||
8 | import org.xwiki.component.util.DefaultParameterizedType | ||
9 | import org.xwiki.model.reference.DocumentReference | ||
10 | import org.xwiki.resource.ResourceReferenceResolver | ||
11 | import org.xwiki.resource.ResourceTypeResolver | ||
12 | import org.xwiki.url.ExtendedURL | ||
13 | |||
14 | // Converts a relative URL path to an EntityReference | ||
15 | DocumentReference convertPathToDocumentReference(String path) { | ||
16 | def serverUrl = xcontext.URLFactory.getServerURL(xcontext.context) | ||
17 | def url = serverUrl.toURI().resolve(path).toURL() | ||
18 | // Code copied from XWiki#initializeResourceFromURL | ||
19 | def extendedUrl = new ExtendedURL(url, xcontext.request.contextPath) | ||
20 | def typeResolver = Utils.getComponent(new DefaultParameterizedType(null, ResourceTypeResolver.class, ExtendedURL.class)) | ||
21 | def type = typeResolver.resolve(extendedUrl, Collections.<String, Object>emptyMap()) | ||
22 | def resourceResolver = Utils.getComponent(new DefaultParameterizedType(null, ResourceReferenceResolver.class, ExtendedURL.class)) | ||
23 | def reference = resourceResolver.resolve(extendedUrl, type, Collections.<String, Object>emptyMap()) | ||
24 | if (reference != null) { | ||
25 | return new DocumentReference(reference.entityReference) | ||
26 | } | ||
27 | return null | ||
28 | } | ||
29 | |||
30 | // Returns the relative path of first attached image, if any | ||
31 | String maybeGetImageUrl(Document page) { | ||
32 | for (Object attachment : page.attachmentList) { | ||
33 | if (attachment.isImage()) { | ||
34 | return page.getAttachmentURL(attachment.filename) | ||
35 | } | ||
36 | } | ||
37 | return null | ||
38 | } | ||
39 | |||
40 | def path = request.path | ||
41 | if (path != null) { | ||
42 | response.setContentType('application/json') | ||
43 | def reference = convertPathToDocumentReference(path) | ||
44 | def content = "" | ||
45 | def imageUrl = null | ||
46 | // Don't render this page itself in case there's a link to it because it will loop | ||
47 | if (reference != doc.documentReference) { | ||
48 | def page = xwiki.getDocument(reference) | ||
49 | content = page.renderedContent | ||
50 | imageUrl = maybeGetImageUrl(page) | ||
51 | } else { | ||
52 | content = "<p>PagePreviewer is an extension used for displaying page preview popovers on page links.</p>" | ||
53 | } | ||
54 | def builder = new JsonBuilder() | ||
55 | builder { | ||
56 | html "$content" | ||
57 | } | ||
58 | if (imageUrl != null) { | ||
59 | builder.content.image = imageUrl | ||
60 | } | ||
61 | println(builder.toString()) | ||
62 | } else { | ||
63 | |||
64 | println "{{info}}" | ||
65 | println "This page contains a script which returns an HTML preview of a given page. By default, it is called automatically for the ##.wikilink## and ##.wikiinternallink## CSS selectors on all pages of the wiki on which it is installed." | ||
66 | println "" | ||
67 | println "The script returns a JSON object containing:" | ||
68 | println "* The rendered content of the page corresponding to the path given as parameter in the request." | ||
69 | println "* The relative URL of the first image attached to that page, if any." | ||
70 | println "" | ||
71 | println "A typical input / output consists of:" | ||
72 | println "* Input: ##/XWiki/PagePreviewer?path=/xwiki/wiki/sandbox/view/Sandbox/&xpage=plain&outputSyntax=plain##" | ||
73 | println "* Output:\n##{\n \"html\": \"<p>Lorem ipsum dolor sit amor.</p>\",\n \"image\": \"/xwiki/wiki/kuava/download/Sandbox/WebHome/XWikiLogo.png?rev=1.1\"\n}##" | ||
74 | println "{{/info}}" | ||
75 | |||
76 | } | ||
77 | {{/groovy}} |