Skip to content

Commit

Permalink
Merge pull request #5 from dylan275/coreDownloadFormats
Browse files Browse the repository at this point in the history
Start adding HTML format for downloading the core
  • Loading branch information
dylan275 authored Feb 7, 2021
2 parents e829620 + 555ef9a commit 2e92b47
Show file tree
Hide file tree
Showing 4 changed files with 334 additions and 16 deletions.
253 changes: 253 additions & 0 deletions out-test1.xsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,253 @@
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:exsl="http://exslt.org/common" extension-element-prefixes="exsl" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" xmlns:tei="http://www.tei-c.org/ns/1.0">

<xsl:output method="html" indent="yes" encoding="UTF-8" />

<!-- définition du texte "de base" : celui qu'on utilisera pour la visualisation html -->
<xsl:variable name="myfile-name">
<xsl:text>29.xml</xsl:text>
</xsl:variable>
<xsl:variable name="myTEI-id" select="tei:TEI/@xml:id" />

<!-- création d'une page html vide -->
<!-- tout ce qui se situe ensuite sert à "remplir" cette page -->
<!-- le fonctionnement se fait généralement par un système de matchs : pour tel élément dans le xml, on crée tel élément dans la page html -->
<xsl:template match="/">
<html>
<head>
<title>
<xsl:value-of select=".//tei:titleStmt/tei:title" />

</title>
<link rel="stylesheet" type="text/css" href="core.css" />
</head>
<body>

<!-- <h3> Contenuto </h3>
<ol>
<xsl:for-each select=".//div">
<li>
<xsl:apply-templates select="head" mode="toc"/>
</li>
</xsl:for-each>
</ol>-->

<table>
<thead>
<tr>
<xsl:for-each select="//tei:titlePage">
<td>

<h1>
<xsl:for-each select="//tei:titlePart">
<xsl:apply-templates />
</xsl:for-each>
</h1>
<h2>
<xsl:for-each select="tei:byline">
<xsl:apply-templates />
</xsl:for-each>
</h2>

</td>
<td class="td-app">
<div id="appbox" />
</td>
<td>
<xsl:apply-templates select=".//tei:pb" mode="facs" />
</td>

</xsl:for-each>
</tr>
</thead>
<xsl:for-each select=".//tei:div">
<tr>
<td class="td-text">
<xsl:apply-templates />
</td>
<td class="td-app">
<div id="appbox" />
</td>
<td style="vertical-align:top">
<xsl:apply-templates select=".//tei:pb" mode="facs" />
</td>
</tr>
</xsl:for-each>
</table>

</body>
</html>
</xsl:template>


<xsl:template match="tei:titlePage" />

<xsl:template match="tei:text">
<xsl:apply-templates />

</xsl:template>
<xsl:template match="tei:div">
<div>
<xsl:apply-templates />
</div>
</xsl:template>
<xsl:template match="tei:titlePage">
<h1>
<xsl:for-each select="tei:titlePart">
<xsl:apply-templates />
</xsl:for-each>
</h1>
<h2>
<xsl:for-each select="tei:byline">
<xsl:apply-templates />
</xsl:for-each>
</h2>
</xsl:template>
<xsl:template match="tei:pb">
[p.
<xsl:value-of select="@n" />
]
</xsl:template>
<xsl:template match="tei:pb" mode="facs">
<xsl:choose>
<xsl:when test="@facs">

<div style="padding: 30px 30px 30px 30px">
<a href="{@facs}" target="_blank">
<img src="{@facs}" style="height:150px; float:right; margin-left:80px" />
</a>
<p style="height:150px; float:right;text-align:right">
[p.
<xsl:value-of select="@n" />
]
</p>
</div>
</xsl:when>
<xsl:otherwise>
<div style="text-align:left">
[p.
<xsl:value-of select="@n" />
]
</div>
</xsl:otherwise>
</xsl:choose>

</xsl:template>

<!-- <xsl:template match="tei:w[ancestor::tei:TEI/@xml:id = $myTEI-id]">-->
<xsl:template match="tei:w">
<xsl:if test="ancestor::tei:TEI/@xml:id = $myTEI-id">
<xsl:variable name="w-id-29" select="@xml:id" />
<span>

<xsl:if test="document('core-1.xml')//tei:ptr[substring-after(@target, '#') = $w-id-29]">
<xsl:attribute name="title">
<xsl:for-each select="document('core-1.xml')//tei:ptr[substring-after(@target, '#') = $w-id-29]">
<xsl:if test="ancestor::tei:app/tei:rdgGrp">

<xsl:choose>
<xsl:when test="ancestor::tei:rdgGrp">
<xsl:for-each select="ancestor::tei:rdgGrp/tei:rdg/tei:ptr[substring-before(@target, '#') != $myfile-name]">
<xsl:variable name="current-filename" select="substring-before(@target, '#')" />

<xsl:variable name="siglum" select="substring-before($current-filename, '.')" />

<xsl:value-of select="$siglum" />
<xsl:text>; </xsl:text>

</xsl:for-each>

</xsl:when>
<xsl:otherwise>
<xsl:for-each select="ancestor::tei:app/tei:rdgGrp">
<xsl:for-each select="tei:rdg[1]/tei:ptr">
<xsl:variable name="current-filename" select="substring-before(@target, '#')" />
<xsl:variable name="siglum" select="substring-before($current-filename, '.')" />
<xsl:variable name="current-w-id" select="substring-after(@target, '#')" />
<xsl:apply-templates select="document($current-filename)//w[@xml:id = $current-w-id]" />
<xsl:text></xsl:text>
<xsl:value-of select="$siglum" />
<xsl:text>, </xsl:text>
<xsl:variable name="currenttarget" select="ancestor::tei:rdgGrp/tei:rdg/tei:ptr[substring-before(@target, '#') != $current-filename]/@target" />

<xsl:value-of select="substring-before(substring-before($currenttarget,'#'),'.')" />
</xsl:for-each>
</xsl:for-each>
</xsl:otherwise>
</xsl:choose>


</xsl:if>
<xsl:if test="ancestor::tei:app/tei:rdg/tei:ptr[substring-before(@target, '#') != $myfile-name]">
<xsl:for-each select="ancestor::tei:app/tei:rdg/tei:ptr[substring-before(@target, '#') != $myfile-name]">
<xsl:variable name="current-filename" select="substring-before(@target, '#')" />
<xsl:variable name="siglum" select="substring-before($current-filename, '.')" />
<xsl:variable name="current-w-id" select="substring-after(@target, '#')" />
<xsl:apply-templates select="document($current-filename)//tei:w[@xml:id = $current-w-id]" />
<xsl:text></xsl:text>
<xsl:value-of select="$siglum" />
<xsl:text>; </xsl:text>
</xsl:for-each>
</xsl:if>
</xsl:for-each>
</xsl:attribute>
<xsl:attribute name="style">
<xsl:text>background-color: #FFFAF0; </xsl:text>
</xsl:attribute>
<xsl:attribute name="onclick">
<xsl:text>document.getElementById('appbox').textContent=getAttribute('title')</xsl:text>
</xsl:attribute>
</xsl:if>
<xsl:apply-templates />
</span>
</xsl:if>
</xsl:template>

<xsl:template match="choice">
<xsl:choose>
<xsl:when test="tei:reg[@type = 'o-atona']">
<i>
<xsl:apply-templates select="tei:orig" />
</i>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="tei:orig" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>


<xsl:template match="tei:choice" mode="toc">
<xsl:apply-templates select="tei:reg" mode="toc" />
</xsl:template>


<xsl:template match="tei:head" mode="toc">
<xsl:apply-templates mode="toc" />
</xsl:template>

<xsl:template match="tei:head">
<h2>
<xsl:apply-templates />
</h2>
</xsl:template>

<xsl:template match="tei:p">
<p>
<xsl:apply-templates />
</p>
</xsl:template>
<xsl:template match="tei:teiHeader" />
<xsl:template match="tei:c">
<xsl:choose>
<xsl:when test="@type = 'o-atona'">
<span style="color:red">
<xsl:apply-templates />
</span>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
9 changes: 8 additions & 1 deletion src/js/templates/core-tpl.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@ let core_tpl = `
<div id="coreHeader">
<div id="coreSubHeader">
<h4 id="ViewCoreLabel">View Core</h4>
<button type="button" class="btn btn-sm btn-primary" id="cb-vc-download"><i class="fa fa-download"></i> Download Core</button>
<div>
<select>
<option>XML</option>
<option>HTML</option>
<option>PDF</option>
</select>
<button type="button" class="btn btn-sm btn-primary" id="cb-vc-download"><i class="fa fa-download"></i> Download Core</button>
</div>
</div>
<button type="button" class="btn btn-secondary" id="toggling">
<i class="fa fa-code"></i>
Expand Down
86 changes: 72 additions & 14 deletions src/js/views/Core-view.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require('bootstrap/dist/js/umd/modal');
class CoreView extends Backbone.View {

initialize() {
this.listenTo(this.collection, "add", this.renderLastEntry)
this.listenTo(this.collection[0], "add", this.renderLastEntry)
}

events() {
Expand All @@ -20,20 +20,78 @@ class CoreView extends Backbone.View {
}

download() {
const edCnt = this.$el.find("#core .cb-ace").get(0);
const edCnt = this.$el.find("#core .cb-ace").get(0)

loadScript("dist/js/libs/ace/ace.js", { scriptTag: true }).then(() => {
var editor;
ace.require(['ace/ace'], (loadedAce) => {
editor = loadedAce.edit(edCnt);
switch (this.$el.find("select")[0].options.selectedIndex) {
case 0:
loadScript("dist/js/libs/ace/ace.js", { scriptTag: true }).then(() => {
var editor;
ace.require(['ace/ace'], (loadedAce) => {
editor = loadedAce.edit(edCnt);

let XML = ""
for (let i = 0; i < editor.getSession().getLength(); i++)
XML += editor.getSession().getLine(i) + '\n'
let XML = ""
for (let i = 0; i < editor.getSession().getLength(); i++)
XML += editor.getSession().getLine(i) + '\n'

saveAs(new Blob([XML], { "type": "text\/xml" }), 'core.xml');
});
});
saveAs(new Blob([XML], { "type": "text\/xml" }), 'core.xml');
});
});

break

case 1:
loadScript("dist/js/libs/ace/ace.js", { scriptTag: true }).then(() => {
var editor;
ace.require(['ace/ace'], (loadedAce) => {
editor = loadedAce.edit(edCnt);

let XML = ""
for (let i = 0; i < editor.getSession().getLength(); i++)
XML += editor.getSession().getLine(i)

let childNodes = (new DOMParser).parseFromString(XML, "application/xml").querySelectorAll("standoff")[0].childNodes
let elementNode = false

childNodes.forEach(childNode => {
if (childNode.nodeType == Node.ELEMENT_NODE)
elementNode = true
})

if (elementNode) {
for (let childNode of childNodes)
if (childNode.nodeType == Node.ELEMENT_NODE) {
let filename

if (!childNode.children[0].attributes[0])
filename = childNode.children[0].children[0].attributes[0].value.substring(1) + ".xml"
else if (childNode.children[0].attributes[0].value[0] == '#')
filename = childNode.children[0].attributes[0].value.substring(1) + ".xml"
else
filename = childNode.children[0].attributes[0].value.substring(0, childNode.children[0].attributes[0].value.indexOf('#'))

for (let XMLFile of this.collection[1].toJSON())
if (XMLFile.filename == filename) {
this.$el.find("#core").append($("<div>").attr("id", "XSLT").hide())
this.$el.find("#core #XSLT").html("")

$.get("out-test1.xsl", function (text) {
let xsltProcessor = new XSLTProcessor()
xsltProcessor.importStylesheet((new DOMParser).parseFromString(text, "application/xml"))
document.getElementById("XSLT").appendChild(xsltProcessor.transformToFragment((new DOMParser).parseFromString(XMLFile.content, "application/xml"), document))

saveAs(new Blob([document.getElementById("XSLT").innerHTML], { "type": "text\/html" }), 'core.html')
}, "text")

break
}

break
}
} else
alert("The core is not complete.")
});
});
}
}

showHTML() {
Expand Down Expand Up @@ -138,7 +196,7 @@ class CoreView extends Backbone.View {
}

renderLastEntry() {
if (this.collection.toJSON()[this.collection.toJSON().length - 2]) {
if (this.collection[0].toJSON()[this.collection[0].toJSON().length - 2]) {
const edCnt = this.$el.find("#core .cb-ace").get(0);

loadScript("dist/js/libs/ace/ace.js", { scriptTag: true }).then(() => {
Expand All @@ -148,7 +206,7 @@ class CoreView extends Backbone.View {

for (let i = 0; i < editor.getSession().getLength(); i++)
if (editor.getSession().getLine(i).includes("</standoff>")) {
let splitedXML = this.collection.toJSON()[this.collection.toJSON().length - 2].xml.split('\n'), XML = ""
let splitedXML = this.collection[0].toJSON()[this.collection[0].toJSON().length - 2].xml.split('\n'), XML = ""
for (let j = 0; j < splitedXML.length; j++) {
XML += '\t'
if (j)
Expand Down
2 changes: 1 addition & 1 deletion src/js/views/app-view.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class CoreBuilder extends Backbone.View {

// Core
this.core = new Core;
this.coreView = new CoreView({ collection: this.core, el: "#workspace" })
this.coreView = new CoreView({ collection: [this.core, xmlFiles], el: "#workspace" })
// Always start the core with one unsaved entry
this.core.add({});
this.listenTo(Events, "coreEntry:addPointer", function (p) { this.core.addPointer(p) });
Expand Down

0 comments on commit 2e92b47

Please sign in to comment.