[xwiki-users] How to programmatically create pages from html files?
Hi all, could someone please help me to find a way to script the creation of new wiki pages from data/text saved in html files? I’m trying to (semi)automatically organize and upload data to new wiki pages, i.e. analyze and plot data with an external program, sve the information (including page name, etc.) in a html file, and then have some sort of script take the html file – or objects from it – and create a wiki page. It doesn’t have to be fully automatic and include a scheduled service or similar, a script that I can run every day would be fine. I did look for hints but I guess I’m too much of a beginner to piece everything together. I found http://extensions.xwiki.org/xwiki/bin/view/Extension/Create+Page+With+Object which tells me that I can script a page creation. Now my naïve question is: Where do I script this? I am not familiar with Velocity (Python is my only scripting language), but willing to learn. From what I could see in tutorials like http://platform.xwiki.org/xwiki/bin/view/DevGuide/XWikiVelocityTraining I put the velocity text in a wiki page (as opposed to run it in a terminal or via some application outside of the wiki pages? Does that mean I create a ‘control’ page with a script that’ll grab data and creates new pages? I f so, it would be great if someone can point me to an example. Thank you Sebastian -- View this message in context: http://xwiki.475771.n2.nabble.com/How-to-programmatically-create-pages-from-... Sent from the XWiki- Users mailing list archive at Nabble.com.
s.schafer wrote:
Does that mean I create a ‘control’ page with a script that’ll grab data and creates new pages? Correct. And you can invoke that from a command-line or cron world if you want with a curl (and some hard-coded passwords). I f so, it would be great if someone can point me to an example. Using groovy is a good idea. It's a tick less limited than using Velocity.
file = new java.io.File("filename.txt"); name = "Space." + file.getName(); doc = xwiki.getDocument(name); println("Saving to document [[${doc}]].") doc.setContent(org.apache.commons.io.FileUtils.readFileToString(file, "utf-8")); doc.setTitle("My beautiful page"); doc.setSyntaxId("xhtml/1.0"); doc.save("Fetching from ${file.getPath()}."); Hope it helps. Paul
I`m not much of a Python fan, but if you say you`re into it, you could always try the http://extensions.xwiki.org/xwiki/bin/view/Extension/Python+Macro and script your way using your favorite language :) If you try it, let me know how it went ;) For scripting API see http://platform.xwiki.org/xwiki/bin/view/DevGuide/API and the available bindings at http://extensions.xwiki.org/xwiki/bin/view/Extension/Script+Macro Now, for your particular use case, you could either create a new wiki page and: 1) use the HTML syntax directly for that wiki page and just dump your HTML content in it or 2) use the rendering script service ($services.rendering, i.e. https://github.com/xwiki/xwiki-platform/blob/master/xwiki-platform-core/xwik...) to parse the HTML content into XDOM and then render it to xwiki/2.1 syntax that you can then save inside the newly created page Depends where you want to go with it. Hope this helps, Eduard On Tue, May 17, 2016 at 10:52 PM, Paul Libbrecht <paul@hoplahup.net> wrote:
s.schafer wrote:
Does that mean I create a ‘control’ page with a script that’ll grab data and creates new pages? Correct. And you can invoke that from a command-line or cron world if you want with a curl (and some hard-coded passwords). I f so, it would be great if someone can point me to an example. Using groovy is a good idea. It's a tick less limited than using Velocity.
file = new java.io.File("filename.txt"); name = "Space." + file.getName(); doc = xwiki.getDocument(name); println("Saving to document [[${doc}]].")
doc.setContent(org.apache.commons.io.FileUtils.readFileToString(file, "utf-8"));
doc.setTitle("My beautiful page"); doc.setSyntaxId("xhtml/1.0"); doc.save("Fetching from ${file.getPath()}.");
Hope it helps.
Paul _______________________________________________ users mailing list users@xwiki.org http://lists.xwiki.org/mailman/listinfo/users
Thank you both, this helped me a lot. I think right now I'm mostly battling the linked images in the original html files. Xwiki uses relative paths and those are not valid anymore after converting to a wiki page. I think I need to handle this when I create the html files and embed the images as text - ugly but the only option I see right now. Cheers Sebastian -----Original Message----- From: users [mailto:users-bounces@xwiki.org] On Behalf Of Eduard Moraru Sent: Tuesday, May 17, 2016 3:54 PM To: XWiki Users Subject: Re: [xwiki-users] How to programmatically create pages from html files? I`m not much of a Python fan, but if you say you`re into it, you could always try the http://extensions.xwiki.org/xwiki/bin/view/Extension/Python+Macro and script your way using your favorite language :) If you try it, let me know how it went ;) For scripting API see http://platform.xwiki.org/xwiki/bin/view/DevGuide/API and the available bindings at http://extensions.xwiki.org/xwiki/bin/view/Extension/Script+Macro Now, for your particular use case, you could either create a new wiki page and: 1) use the HTML syntax directly for that wiki page and just dump your HTML content in it or 2) use the rendering script service ($services.rendering, i.e. https://github.com/xwiki/xwiki-platform/blob/master/xwiki-platform-core/xwik...) to parse the HTML content into XDOM and then render it to xwiki/2.1 syntax that you can then save inside the newly created page Depends where you want to go with it. Hope this helps, Eduard On Tue, May 17, 2016 at 10:52 PM, Paul Libbrecht <paul@hoplahup.net> wrote:
s.schafer wrote:
Does that mean I create a ‘control’ page with a script that’ll grab data and creates new pages? Correct. And you can invoke that from a command-line or cron world if you want with a curl (and some hard-coded passwords). I f so, it would be great if someone can point me to an example. Using groovy is a good idea. It's a tick less limited than using Velocity.
file = new java.io.File("filename.txt"); name = "Space." + file.getName(); doc = xwiki.getDocument(name); println("Saving to document [[${doc}]].")
doc.setContent(org.apache.commons.io.FileUtils.readFileToString(file, "utf-8"));
doc.setTitle("My beautiful page"); doc.setSyntaxId("xhtml/1.0"); doc.save("Fetching from ${file.getPath()}.");
Hope it helps.
Paul _______________________________________________ users mailing list users@xwiki.org http://lists.xwiki.org/mailman/listinfo/users
_______________________________________________ users mailing list users@xwiki.org http://lists.xwiki.org/mailman/listinfo/users
Sebastian Schafer wrote:
I think right now I'm mostly battling the linked images in the original html files. Xwiki uses relative paths and those are not valid anymore after converting to a wiki page. I think I need to handle this when I create the html files and embed the images as text - ugly but the only option I see right now. Here would be a continuation, before the document.save of course. The imports should be put first.
paul import java.util.regexp.*; import org.apache.commons.io.FileUtils; import java.io.File; import com.xpn.xwiki.api.*; java.util.regexp.Pattern pattern = Pattern.compile("<img src=\\\"(/download/attachments/[^\\\"]*)"); Matcher m = pattern.matcher(html); println("<ul>") while(m.find()) { URL u = new URL(pageURL, m.group(1)); String imageName = u.getPath().replaceAll(".*/",""); println("<li><a href='${u}'>${imageName }</a>"); File tmp = new File("/tmp/xxx.png"); FileUtils.copyURLToFile(u, tmp); // now fetch and store byte[] content = FileUtils.readFileToByteArray(tmp); //println("Attachment is of size ${content.length} and is named ${name}"); Attachment attachment = doc.addAttachment(imageName , content); println(" has become <a href='${doc.getAttachmentURL(imageName )}'>${imageName }</a> (rev ${attachment.getVersion()}).</li>"); } println("</ul>");
participants (4)
-
Eduard Moraru -
Paul Libbrecht -
s.schafer -
Sebastian Schafer