cogDownload default LaTeX templates; Upload/download LaTeX templates to/from a skin page.
Developed by

Guido Kracke

Rating
Rate!
0 Votes
LicenseGNU Lesser General Public License 2.1

Description

Usage:

  • Paste the following code to an empty page
  • Select LaTeX <version> as LaTeX Templates and press Download LaTeX Templates to download a zip file with the default templates
  • Extract the template files and make you changes
  • Zip the changed files preserving the directory structure
  • Attach the zip file to the page
  • Select, e.g., XWiki.DefaultSkin as skin and the attachment as Upload from Attachment and press Upload LaTeX Templates to upload the changed templates to the skin

If there is an easy way to do the upload without attaching the zip file, I'd like to know.

{{groovy}}
import com.xpn.xwiki.api.Attachment
import com.xpn.xwiki.api.Document
import com.xpn.xwiki.api.Object
import com.xpn.xwiki.web.XWikiServletResponse
import java.nio.charset.Charset
import java.util.zip.ZipEntry
import java.util.zip.ZipInputStream
import java.util.zip.ZipOutputStream
import org.xwiki.extension.InstalledExtension
import org.xwiki.query.Query

String CHARSET_NAME = "UTF-8"
Charset CHARSET = Charset.forName(CHARSET_NAME)
String XCLASS = "XWiki.XWikiSkinFileOverrideClass"
String LATEX = null
InstalledExtension latex = services.extension.installed.getInstalledExtension("org.xwiki.contrib.latex:latex-export", "wiki:xwiki")
if (latex != null) {
    LATEX = "LaTeX " + latex.getId().getVersion().getValue()
}

String page = request.getParameter("page")
String file = request.getParameter("file")
Query query = services.query.hql("""
    select d.fullName
    from XWikiDocument as d, BaseObject as o
    where d.fullName = o.name
    and o.className = :class
"""
)
ArrayList<String> pages = query.bindValue("class", "XWiki.XWikiSkins").execute()
StringBuffer out = new StringBuffer()
out << """
{{html}}
<form action="" id="downloadLatex" method="post">
  <div>
    <table style="width:auto">
      <tbody>
        <tr>
          <th style="border: 1px solid #ddd;padding: 3px;background: #eee">
            LaTeX Templates
          </th>
          <td style="border: 1px solid #ddd;padding: 3px">
            <select id="page" name="page" onchange="onTemplates(this.value)" onload="onTemplates(this.value)">
"""

if (LATEX != null) {
    out << "<option"
   if (LATEX == page) {
        out << " selected"
   }
    out << ">" << LATEX << "</option>\n"
}
for (p in pages) {
   if (p != "XWiki.XWikiSkinsTemplate") {
        out << "<option"
       if (p == page) {
            out << " selected"
       }
        out << ">" << p << "</option>\n"
   }
}
out << """
            </select>
          </td>
        </tr>
        <tr id="filerow" style="display:none">
          <th style="border: 1px solid #ddd;padding: 3px;background: #eee">
            Upload from Attachment
          </th>
          <td style="border: 1px solid #ddd;padding: 3px">
            <select id="file" name="file" size=1 onchange="onAttachment(this.value)" onload="onAttachment(this.value)">
"""

if (file == null || file == "") {
    out << "<option selected></option>"
}
else {
    out << "<option></option>"
}
for (att in doc.getAttachmentList()) {
    out << "<option"
   if (file != null && att.getFilename().equals(file)) {
        out << " selected"
   }
    out << ">" << att.getFilename() << "</option>\n"
}
out << """
            </select>
          </td>
        </tr>
      </tbody>
    </table>
"""

if (file == null || file == "") {
    out << """<span class="buttonwrapper"><input id="btn1" type="submit" value="Download LaTeX Templates" class="button"/></span>"""
} else {
    out << """<span class="buttonwrapper"><input id="btn1" type="submit" value="Upload LaTeX Templates" class="button"/></span>"""
}
out << """
  </div>
</form>
<script type="text/javascript">
function onAttachment(val) {
    if (val == null || val == "") {
        document.getElementById("btn1").value="Download LaTeX Templates"
    } else {
        document.getElementById("btn1").value="Upload LaTeX Templates"
    }
}
function onTemplates(val) {
    if (val.startsWith("LaTeX ")) {
        document.getElementById("file").value=""
        document.getElementById("filerow").style.display="none"
        document.getElementById("btn1").value="Download LaTeX Templates"
    }
    else {
        document.getElementById("filerow").style.display=""
    }
}
</script>

{{/html}}

"""

print out.toString()

if (page != null) {
   if (file == "") {
        XWikiServletResponse response = xcontext.getResponse()
        response.setContentType("application/zip")
        response.setHeader("Content-disposition", "attachment; filename=latex.zip")
        ZipOutputStream zip = new ZipOutputStream(response.getOutputStream(), CHARSET)
       if (page == LATEX) {
           for (extension in services.extension.installed.getInstalledExtensions()) {
               if (extension.getId().getId().startsWith("org.xwiki.contrib.latex:")) {
                    ZipInputStream jar = new ZipInputStream(extension.getLocalExtension().getFile().openStream(), CHARSET)
                    ZipEntry entry = jar.getNextEntry()
                   while (entry != null) {
                        String path = entry.getName()
                       if (path.startsWith("templates/latex/default/") && !path.endsWith("/")) {
                            ByteArrayOutputStream bos = new ByteArrayOutputStream()
                           byte[] buffer = new byte[2048]
                           int len = jar.read(buffer)
                           while (len > 0) {
                                bos.write(buffer, 0, len)
                                len = jar.read(buffer)
                           }
                            bos.close()
                            zip.putNextEntry(new ZipEntry(path.replace("templates/latex/default/", "latex/")))
                            buffer = bos.toByteArray()
                            zip.write(buffer, 0, buffer.length)
                            zip.closeEntry()
                       }
                        entry = jar.getNextEntry()
                   }
                    jar.close()
               }
           }
       }
       else {
            Document skin = xwiki.getDocument("xwiki:" + page)
           for (Object o : skin.getObjects(XCLASS)) {
                String path = o.getValue("path")
                String content = o.getValue("content")
               byte[] bytes = content.getBytes(CHARSET_NAME)
                zip.putNextEntry(new ZipEntry(path))
                zip.write(bytes, 0, bytes.length)
                zip.closeEntry()
           }
       }
        zip.close()
        response.flushBuffer()
   }
   else {
        Map<String, Object> templates = new HashMap<>()
        Document skin = xwiki.getDocument("xwiki:" + page)
       for (Object o : skin.getObjects(XCLASS)) {
            templates.put(o.getValue("path"), o)
       }
        Attachment att = doc.getAttachment(file)
        ZipInputStream zip = new ZipInputStream(att.getContentInputStream(), CHARSET)
        ZipEntry entry = zip.getNextEntry()
       while (entry != null) {
           if (!entry.isDirectory()) {
                Object template = templates.get(entry.getName())
               if (template == null) {
                    template = skin.newObject(XCLASS)
                    template.set("path", entry.getName())
               }
                ByteArrayOutputStream bos = new ByteArrayOutputStream()
               byte[] buffer = new byte[2048]
               int len = zip.read(buffer)
               while (len > 0) {
                    bos.write(buffer, 0, len)
                    len = zip.read(buffer)
               }
                bos.close()
                template.set("content", bos.toString(CHARSET_NAME))
           }
            entry = zip.getNextEntry()
       }
        zip.close()
        skin.save()
        response.sendRedirect(xwiki.getURL(doc.getFullName()))
   }
}
{{/groovy}}

Prerequisites & Installation Instructions

Copy the code snippet to a page and save it.

Tags: latex
Created by Guido Kracke on 2019/10/29 17:00
     

Get Connected