### Local Web Server Commands Source: https://github.com/teic/ceteicean/blob/master/tutorial_en/README.md Commands to launch a local development server for testing TEI documents in a browser. These are useful when local file access is restricted by browser security policies. ```bash python3 -m http.server ``` ```bash npx serve ``` -------------------------------- ### Project Build and Development Commands Source: https://github.com/teic/ceteicean/blob/master/README.md Standard CLI commands for installing dependencies, building the project distribution files, and running a local development server. ```bash npm i npm run build npm run dev ``` -------------------------------- ### Instantiate CETEI Class Constructor (JavaScript) Source: https://context7.com/teic/ceteicean/llms.txt Demonstrates how to instantiate the main CETEI class with various configuration options. These options control document handling, base URLs, debugging, and default behaviors. It includes examples for basic browser instantiation, instantiation with options, and server-side instantiation using JSDOM. ```javascript // Basic browser instantiation const ct = new CETEI(); // With options const ct = new CETEI({ base: "https://example.com/tei-files/", // Base URL for resolving relative links debug: true, // Enable debug logging omitDefaultBehaviors: false, // Set true to skip default behaviors ignoreFragmentId: true, // Disable scroll position restoration discardContent: false // Set true to discard original content when applying behaviors }); // Server-side with JSDOM import { JSDOM } from 'jsdom'; import CETEI from 'CETEIcean'; const jdom = new JSDOM(``, {contentType: 'text/xml'}); const ct = new CETEI({ documentObject: jdom.window.document }); ``` -------------------------------- ### Complete HTML Page Integration for CETEIcean Source: https://context7.com/teic/ceteicean/llms.txt A comprehensive example showing the necessary HTML structure, CSS for TEI element styling, and JavaScript initialization. It includes custom behavior definitions for specific TEI tags and the method to fetch and render an XML document. ```html TEI Document Viewer
Loading TEI document...
``` -------------------------------- ### CETEIcean Behavior Configuration Source: https://github.com/teic/ceteicean/blob/master/tutorial_en/README.md JavaScript configuration to define custom behaviors for TEI elements. This example demonstrates how to replace a TEI line break element with an HTML break tag. ```javascript let c = new CETEI(); let behaviors = { "tei": { "lb": ["
"], } }; c.addBehaviors(behaviors); c.getHTML5('fpn-washington.xml', function(data) { document.getElementsByTagName("body")[0].appendChild(data); }); ``` -------------------------------- ### Initialize CETEIcean and Render TEI XML Source: https://github.com/teic/ceteicean/blob/master/tutorial_en/README.md This JavaScript code initializes a CETEI object and uses it to load and transform a TEI XML file into HTML5 elements. The resulting HTML is then appended to the body of the current HTML document. This snippet requires the CETEIcean library to be included. ```javascript let c = new CETEI(); c.getHTML5('fpn-washington.xml', function(data) { document.getElementsByTagName("body")[0].appendChild(data); }); ``` -------------------------------- ### GET /getHTML5 Source: https://github.com/teic/ceteicean/blob/master/README.md Fetches a TEI XML document from a URL and converts it into HTML5 Custom Elements. ```APIDOC ## GET getHTML5 ### Description Fetches an XML source document from a URL and converts it into HTML5 Custom Elements. ### Method GET ### Parameters #### Query Parameters - **url** (string) - Required - The URL of the TEI XML document to fetch. - **callback** (function) - Optional - A function to be called on the results. - **perElementFn** (function) - Optional - A function to be called on each resulting element. ### Request Example ```js var CETEIcean = new CETEI(); CETEIcean.getHTML5("URL_TO_YOUR_TEI.xml", function(data) { document.getElementById("TEI").appendChild(data); }); ``` ### Response #### Success Response (200) - **data** (HTMLElement) - The resulting DOM element containing the converted TEI content. ``` -------------------------------- ### Define 'lb' behavior for CETEIcean Source: https://github.com/teic/ceteicean/blob/master/tutorial_en/README.md This snippet defines a behavior for the TEI 'lb' element. It maps 'lb' to an array containing '
', instructing CETEIcean to replace `` elements with `
` tags. This is a simple, static replacement. ```javascript let behaviors = { "tei": { "lb": ["
"] } }; c.addBehaviors(behaviors); ``` -------------------------------- ### Basic HTML Structure for TEI Publishing Source: https://github.com/teic/ceteicean/blob/master/tutorial_en/README.md This HTML code serves as a basic shell for displaying TEI content. It includes meta tags for character set and viewport, and links to external CSS and JavaScript files. The body is initially empty, ready to be populated by the rendered TEI content. ```html ``` -------------------------------- ### Initialize CETEI.js and Add Custom TEI Behaviors Source: https://github.com/teic/ceteicean/blob/master/tutorial_en/example/index.html Initializes a CETEI.js instance and defines custom behaviors for TEI elements like 'head' and 'pb'. The 'head' behavior creates HTML heading tags based on the element's nesting level, and 'pb' creates a paragraph with a CSS class. This setup allows for customized rendering of TEI XML. ```javascript let c = new CETEI(); let behaviors = { "tei": { "head": function(e) { let level = document.evaluate("count(ancestor::tei-div)", e, null, XPathResult.NUMBER_TYPE, null); let result = document.createElement("h" + (level.numberValue > 7 ? 7 : level.numberValue)); for (let n of Array.from(e.childNodes)) { result.appendChild(n.cloneNode()); } return result; }, "lb": ["
"], "pb": ["

$@n

"] } }; c.addBehaviors(behaviors); ``` -------------------------------- ### CSS Styling for TEI Elements Source: https://github.com/teic/ceteicean/blob/master/tutorial_en/README.md CSS rules to format TEI elements rendered as custom HTML elements. These include block display settings, list formatting, and pseudo-selectors for line breaks. ```css tei-div { display: block; } tei-p { display: block; margin-top: .5em; margin-bottom: .5em; } tei-list[type=simple] { list-style-type: none; } tei-list[type=simple]>tei-item { display: list-item; } tei-hi[rend=italics] { font-style: italic; } tei-lb:before { white-space: pre; content: "\A"; } ``` -------------------------------- ### Convert XML String to HTML5 (JavaScript) Source: https://context7.com/teic/ceteicean/llms.txt Details the `makeHTML5` method, which converts an XML string directly into HTML5 Custom Elements. This is useful when the TEI content is already available as a string. The example demonstrates passing a TEI XML string and handling the converted output via a callback function. ```javascript const ct = new CETEI(); const teiString = ` Sample Document

Published digitally

Born digital

This is a sample paragraph.

`; ct.makeHTML5(teiString, function(data) { document.getElementById("TEI").appendChild(data); }); ``` -------------------------------- ### Convert XML DOM to HTML5 (JavaScript) Source: https://context7.com/teic/ceteicean/llms.txt Presents the `domToHTML5` method for converting an XML DOM object into HTML5 Custom Elements. This method is ideal for server-side processing or when working with pre-parsed XML documents. Examples cover browser usage with `DOMParser` and Node.js usage with `JSDOM`, including querying the processed document. ```javascript // Browser usage with DOMParser const ct = new CETEI(); const parser = new DOMParser(); const xmlDoc = parser.parseFromString(teiString, "text/xml"); ct.domToHTML5(xmlDoc, function(data) { document.getElementById("TEI").appendChild(data); }); // Node.js usage with JSDOM import { JSDOM } from 'jsdom'; import CETEI from 'CETEIcean'; import fs from 'fs'; const tei = fs.readFileSync('document.xml', 'utf8'); const jdom = new JSDOM(tei, {contentType: 'text/xml'}); const teiDoc = jdom.window.document; const ct = new CETEI({documentObject: teiDoc}); const processedTEI = ct.domToHTML5(teiDoc); // Query the processed document const paragraphs = processedTEI.querySelectorAll("tei-p"); console.log(`Found ${paragraphs.length} paragraphs`); ``` -------------------------------- ### Override built-in 'teiHeader' behavior in CETEIcean Source: https://github.com/teic/ceteicean/blob/master/tutorial_en/README.md This snippet demonstrates how to override a built-in CETEIcean behavior by setting the 'teiHeader' behavior to null within the behaviors object. This effectively makes the TEI Header content visible, although additional CSS or behaviors might be needed to style it appropriately. ```javascript let behaviors = { "tei": { "teiHeader": null } }; c.addBehaviors(behaviors); ``` -------------------------------- ### Define dynamic 'head' behavior for CETEIcean Source: https://github.com/teic/ceteicean/blob/master/tutorial_en/README.md This snippet defines a dynamic behavior for the TEI 'head' element using a JavaScript function. The function determines the nesting level of `` ancestors and creates an appropriate HTML heading element (e.g., `

`, `

`). It then copies the child nodes from the TEI element to the new heading element. ```javascript let behaviors = { "tei": { "head": function(e) { let level = document.evaluate("count(ancestor::tei-div)", e, null, XPathResult.NUMBER_TYPE, null); let result = document.createElement("h" + level.numberValue); for (let n of Array.from(e.childNodes)) { result.appendChild(n.cloneNode()); } return result; }, "lb": ["
"] } }; c.addBehaviors(behaviors); ``` -------------------------------- ### Configuration / Constructor Source: https://github.com/teic/ceteicean/blob/master/README.md Initializes the CETEIcean instance with specific configuration options. ```APIDOC ## Constructor ### Description Instantiates a new CETEI object with optional configuration settings. ### Parameters #### Request Body - **ignoreFragmentId** (boolean) - Optional - If true, disables saving/restoring scroll position via URL fragment. - **documentObject** (object) - Optional - Provides a custom DOM implementation (e.g., JSDOM for Node.js environments). ``` -------------------------------- ### Initialize CETEIcean with custom behaviors Source: https://github.com/teic/ceteicean/wiki/Anatomy-of-a-behaviors-object Demonstrates how to instantiate the CETEI object, inject custom behavior logic, and process an XML file into the DOM. ```html ``` -------------------------------- ### Initialize and Render TEI Documents Source: https://github.com/teic/ceteicean/blob/master/README.md Demonstrates how to instantiate the CETEIcean object and fetch a TEI XML file to render it within a specific DOM element. This is the standard approach for client-side document display. ```javascript var CETEIcean = new CETEI() CETEIcean.getHTML5("URL_TO_YOUR_TEI.xml", function(data) { document.getElementById("TEI").appendChild(data) }) ``` -------------------------------- ### Configure CETEIcean Options Source: https://github.com/teic/ceteicean/blob/master/README.md Shows how to pass configuration objects during initialization, such as disabling fragment ID tracking for server-side rendering scenarios. ```javascript new CETEI({ ignoreFragmentId: true }) ``` -------------------------------- ### CETEI Class Constructor Source: https://context7.com/teic/ceteicean/llms.txt Instantiate the main CETEI class with an optional configuration object to control document handling, base URLs, debugging, and default behaviors. ```APIDOC ## CETEI Class Constructor The main CETEI class is instantiated with an optional configuration object that controls document handling, base URLs, debugging, and default behaviors. ### Basic Browser Instantiation ```javascript const ct = new CETEI(); ``` ### Instantiation with Options ```javascript const ct = new CETEI({ base: "https://example.com/tei-files/", // Base URL for resolving relative links debug: true, // Enable debug logging omitDefaultBehaviors: false, // Set true to skip default behaviors ignoreFragmentId: true, // Disable scroll position restoration discardContent: false // Set true to discard original content when applying behaviors }); ``` ### Server-Side Instantiation with JSDOM ```javascript import { JSDOM } from 'jsdom'; import CETEI from 'CETEIcean'; const jdom = new JSDOM(``, {contentType: 'text/xml'}); const ct = new CETEI({ documentObject: jdom.window.document }); ``` ``` -------------------------------- ### Initialize CETEIcean and Register Custom TEI Behaviors Source: https://github.com/teic/ceteicean/blob/master/test/TCW22.html This code initializes a new CETEI instance and defines a behavior mapping to transform 'tei-title' elements into 'tei-head' elements within the document body. It then fetches a remote TEI XML file and appends the processed result to the document body. ```javascript let ct = new CETEI(); let b = { "tei": { "title": [ ["tei-titlestmt>tei-title", function(e) { let head = document.createElement("tei-head"); head.innerHTML = e.innerHTML; let body = this.dom.querySelector("tei-body"); body.insertBefore(head, body.firstElementChild); }] ] } }; ct.addBehaviors(b); ct.getHTML5('https://raw.githubusercontent.com/TEIC/Documentation/master/TCW/tcw22.xml?new=' + Date.now(), function(data) { document.getElementsByTagName("body")[0].appendChild(data); }); ``` -------------------------------- ### Fetch and Convert TEI to HTML5 (JavaScript) Source: https://context7.com/teic/ceteicean/llms.txt Explains the `getHTML5` method for fetching TEI XML from a URL and converting it into HTML5 Custom Elements. It shows how to use both callback functions and Promises for handling the conversion result, and how to optionally provide a function for per-element processing during conversion. ```javascript // Using callback const ct = new CETEI(); ct.getHTML5('path/to/document.xml', function(data) { document.getElementById("TEI").appendChild(data); }); // Using Promise const ct = new CETEI(); ct.getHTML5('path/to/document.xml').then(function(data) { document.getElementById("TEI").innerHTML = ""; document.getElementById("TEI").appendChild(data); }); // With per-element processing function ct.getHTML5('document.xml', function(data) { document.getElementById("TEI").appendChild(data); }, function(newElement, originalElement) { // Called for each element during conversion console.log('Converted:', originalElement.localName, '->', newElement.tagName); }); ``` -------------------------------- ### getHTML5(url, callback, perElementFn) Source: https://context7.com/teic/ceteicean/llms.txt Fetches a TEI XML document from a URL, converts it to HTML5 Custom Elements, and returns the result via callback or Promise. This is the primary method for loading and displaying TEI documents in a web browser. ```APIDOC ## getHTML5(url, callback, perElementFn) Fetches a TEI XML document from a URL, converts it to HTML5 Custom Elements, and returns the result via callback or Promise. This is the primary method for loading and displaying TEI documents in a web browser. ### Using Callback ```javascript const ct = new CETEI(); ct.getHTML5('path/to/document.xml', function(data) { document.getElementById("TEI").appendChild(data); }); ``` ### Using Promise ```javascript const ct = new CETEI(); ct.getHTML5('path/to/document.xml').then(function(data) { document.getElementById("TEI").innerHTML = ""; document.getElementById("TEI").appendChild(data); }); ``` ### With Per-Element Processing Function ```javascript ct.getHTML5('document.xml', function(data) { document.getElementById("TEI").appendChild(data); }, function(newElement, originalElement) { // Called for each element during conversion console.log('Converted:', originalElement.localName, '->', newElement.tagName); }); ``` ``` -------------------------------- ### POST /makeHTML5 Source: https://github.com/teic/ceteicean/blob/master/README.md Converts a provided XML string directly into HTML5 Custom Elements. ```APIDOC ## POST makeHTML5 ### Description Converts the supplied XML string into HTML5 Custom Elements. ### Method POST ### Parameters #### Request Body - **XML** (string) - Required - The XML document serialized to a string. - **callback** (function) - Optional - A function to be called on the results. - **perElementFn** (function) - Optional - A function to be called on each resulting element. ### Response #### Success Response (200) - **data** (HTMLElement) - The resulting DOM element containing the converted TEI content. ``` -------------------------------- ### Configure namespaces for non-standard TEI Source: https://github.com/teic/ceteicean/wiki/Anatomy-of-a-behaviors-object Shows how to handle documents without namespaces or override existing namespace mappings by unsetting them first. ```javascript { "namespaces": { "teip4": "" } } let c = new CETEI(); c.unsetNamespace("http://www.tei-c.org/ns/1.0"); c.addBehaviors({"namespaces": {"tei":""}, "tei": { } }) ``` -------------------------------- ### Initialize CETEI and Add Behaviors (JavaScript) Source: https://github.com/teic/ceteicean/blob/master/tutorial_es/example/index.html This snippet demonstrates how to initialize a CETEI object and add custom behaviors for processing TEI elements. It defines rules for 'head' (converting to heading tags), 'note' (transforming into endnotes with links), and other elements like 'placeName', 'persName', and 'rs' (creating links based on 'ref' attributes). ```javascript let c = new CETEI(); let behaviors = { "tei": { "head": function(e) { let level = document.evaluate("count(ancestor::tei-div)", e, null, XPathResult.NUMBER_TYPE, null); let result = document.createElement("h" + (level.numberValue < 7 ? level.numberValue : 6)); for (let n of Array.from(e.childNodes)) { result.appendChild(n.cloneNode()); } return result; }, "note": function(e){ if (!this.noteIndex){ this["noteIndex"] = 1; } else { this.noteIndex++; } let id = "note" + this.noteIndex; let link = document.createElement("a"); link.setAttribute("id", "src" + id); link.setAttribute("href", "#" + id); link.innerHTML = this.noteIndex; let content = document.createElement("sup"); if (e.previousSibling.localName == "tei-note") { content.appendChild(document.createTextNode(",")); } content.appendChild(link); let notes = this.dom.querySelector("ol.notes"); if (!notes) { notes = document.createElement("ol"); notes.setAttribute("class", "notes"); this.dom.appendChild(notes); } let note = document.createElement("li"); note.id = id; note.innerHTML = "^ " + e.innerHTML; notes.appendChild(note); return content; }, "placeName": [ ["[ref]", ["",""] ], "persName": [ ["[ref]", ["",""] ], "rs": [ ["[ref]", ["",""] ] } }; c.addBehaviors(behaviors); ``` -------------------------------- ### Initialize CETEIcean and Custom Behaviors Source: https://github.com/teic/ceteicean/blob/master/test/EEBOTest.html JavaScript implementation to instantiate the CETEIcean object, define custom element behaviors for 'gap', 'note', and 'pb' tags, and load external TEI XML data. It includes logic for handling EEBO image fetching and browser-specific rendering fixes. ```javascript var CETEIcean = new CETEI(); var images = []; CETEIcean.addBehaviors({"tei": { "gap": ["[", "]"], "note": function(elt) { var span = document.createElement("span"); span.innerHTML = elt.getAttribute("place") == "margin"?"[in marg. ":"["; span.innerHTML += elt.innerHTML + "]"; return span; }, "pb": function(elt) { var span = document.createElement("span"); var link = document.createElement("a"); link.setAttribute("style", "float:right;padding-left:1em;text-decoration:none;color:gray;"); if (elt.hasAttribute("facs")) { var facs = elt.getAttribute("facs").replace("tcp:", "").replace(new RegExp(this.getPrefixDef("tcp")["matchPattern"]), "?vid=$1&page=$2"); link.setAttribute("href", "http://eebo.chadwyck.com/fetchimage" + facs + "&width=1500"); var img = document.createElement("img"); images.push(img); img.setAttribute("src", "http://eebo.chadwyck.com/fetchimage" + facs + "&width=100"); img.setAttribute("height", "50px"); if (elt.hasAttribute("n")) { link.innerHTML = "p. " + elt.getAttribute("n") + " "; } link.appendChild(img); } span.appendChild(document.createElement("br")); span.appendChild(link); return span; } }}); CETEIcean.getHTML5('https://raw.githubusercontent.com/textcreationpartnership/A00689/master/A00689.xml', function(data) { document.getElementById("TEI").innerHTML = ""; document.getElementById("TEI").appendChild(data); if (!!window.chrome) { var gs = document.getElementsByTagName("tei-g"); for (var i = 0; i < gs.length; i++) { if (gs[i].getAttribute("ref") == "char:cmbAbbrStroke") { gs[i].setAttribute("class", "cmbAbbr"); } } } }); ``` -------------------------------- ### Server-Side Rendering with JSDOM Source: https://github.com/teic/ceteicean/blob/master/README.md Illustrates how to use CETEIcean in a Node.js environment by providing a JSDOM document object, allowing for server-side processing of TEI documents. ```javascript import { JSDOM } from 'jsdom'; import CETEI from 'CETEIcean'; const jdom = new JSDOM(``, {contentType: 'text/xml'}); new CETEI({ documentObject: jdom.window.document }) ``` -------------------------------- ### Configure CETEIcean behaviors and load TEI document Source: https://github.com/teic/ceteicean/blob/master/tutorial_ja/example/index.html This script initializes the CETEIcean library and defines custom rendering behaviors for specific TEI elements. It uses XPath to determine heading levels and maps tags to HTML equivalents, followed by loading and appending the processed document to the body. ```javascript let c = new CETEI(); let behaviors = { "tei": { "head": function(e) { let level = document.evaluate("count(ancestor::tei-div)", e, null, XPathResult.NUMBER_TYPE, null); let result = document.createElement("h" + (level.numberValue > 7 ? 7 : level.numberValue)); for (let n of Array.from(e.childNodes)) { result.appendChild(n.cloneNode()); } return result; }, "lb": ["
"], "pb": ["

$@n

"] } }; c.addBehaviors(behaviors); c.getHTML5('../fpn-washington.xml', function(data) { document.getElementsByTagName('body')[0].appendChild(data); }); ``` -------------------------------- ### Load and Display XML with CETEIcean (JavaScript) Source: https://github.com/teic/ceteicean/blob/master/test/addBehaviorTest.html This JavaScript code snippet shows how to load an XML file ('testTEI.xml') using CETEIcean and display its HTML5 representation. It uses the `getHTML5` method, which accepts a callback function to handle the loaded data. The content is then appended to an element with the ID 'TEI'. An alternative using Promises (`then()`) is also commented out. ```javascript c.getHTML5('testTEI.xml', function(data) { document.getElementById("TEI").innerHTML = ""; document.getElementById("TEI").appendChild(data); }); // Alternatively, use then() // (new CETEI).getHTML5('testTEI.xml').then(function(data){ // document.getElementById("TEI").appendChild(data); // }); ``` -------------------------------- ### Insert Static Content Decorators in JavaScript Source: https://github.com/teic/ceteicean/wiki/Anatomy-of-a-behaviors-object Demonstrates how to insert static content before and after an element's original content using a simple array of strings. If the strings contain no markup, they are wrapped in a ``. The first item is inserted before, and the second after, with the original content preserved in a hidden ``. ```javascript "add": ["`","´"] ``` -------------------------------- ### Implement endnote behavior with bidirectional linking Source: https://context7.com/teic/ceteicean/llms.txt Shows how to use addBehaviors to transform inline TEI notes into endnotes. It creates a list of notes at the end of the document and links them back to their original positions. ```javascript const ct = new CETEI(); ct.addBehaviors({ "tei": { "note": [ ["[place=end]", function(elt) { if (!this.noteIndex) { this.noteIndex = 1; } else { this.noteIndex++; } const id = "_note_" + this.noteIndex; const doc = elt.ownerDocument; // Create superscript link in text const link = doc.createElement("a"); link.setAttribute("id", "src" + id); link.setAttribute("href", "#" + id); link.innerHTML = this.noteIndex; const content = doc.createElement("sup"); content.appendChild(link); // Create or find notes container let notes = doc.querySelector("ol.notes"); if (!notes) { notes = doc.createElement("ol"); notes.setAttribute("class", "notes"); this.dom.appendChild(notes); } // Add note to endnotes list const note = doc.createElement("li"); note.id = id; note.innerHTML = "^ " + elt.innerHTML; notes.appendChild(note); return content; }], ["_", ["(", ")"]] ] } }); ``` -------------------------------- ### Utilize CETEIcean utility functions for element manipulation Source: https://context7.com/teic/ceteicean/llms.txt Demonstrates the use of cetei.utilities for copying, serializing, and managing TEI elements. It includes methods for URL rewriting, ordinality calculation, and content hiding. ```javascript const ct = new CETEI(); ct.getHTML5('document.xml', function(data) { document.getElementById("TEI").appendChild(data); const element = document.querySelector("tei-p"); // Copy and reset: create a fresh copy without processing flags const freshCopy = ct.utilities.copyAndReset(element); // Serialize to XML string const xmlString = ct.utilities.serialize(element); console.log(xmlString); // Serialize with pretty-printing const prettyXml = ct.utilities.serialize(element, false, ""); // Combined reset and serialize const cleanXml = ct.utilities.resetAndSerialize(element, false, ""); // Rewrite relative URL const absoluteUrl = ct.utilities.rw("images/figure1.png"); // Get first URL from space-separated list const firstUrl = ct.utilities.first("url1.html url2.html url3.html"); // Get element ordinality (position among siblings) const position = ct.utilities.getOrdinality(element, "tei-p"); // Hide element content (wrap in hidden span) ct.utilities.hideContent(element); }); ``` -------------------------------- ### Initialize CETEICEAN and Define Custom TEI Behaviors Source: https://github.com/teic/ceteicean/blob/master/tutorial_ja/example/index_ja.html This snippet initializes the CETEI library and applies custom rendering logic for TEI elements. It uses XPath to determine heading levels and replaces standard TEI tags with custom HTML5 structures, including ruby annotations. ```javascript let c = new CETEI(); let behaviors = { "tei": { "head": function(e) { let level = document.evaluate("count(ancestor::tei-div)", e, null, XPathResult.NUMBER_TYPE, null); let result = document.createElement("h" + (level.numberValue > 7 ? 7 : level.numberValue)); for (let n of Array.from(e.childNodes)) { result.appendChild(n.cloneNode()); } return result; }, "lb": ["
"], "pb": ["

$@n

"], } }; c.addBehaviors(behaviors); c.getHTML5('../meros.xml', function(data) { document.getElementsByTagName('body')[0].appendChild(data); let ruby = document.querySelectorAll('tei-ruby'); ruby.forEach(function(e1) { let rbt = e1.querySelectorAll('tei-rb,tei-rt'); let ruby = document.createElement('ruby'); rbt.forEach(function(e2) { let rbt = document.createElement(e2.tagName.replace(/TEI-/, '')); rbt.textContent = e2.textContent; ruby.append(rbt); }); e1.replaceWith(ruby); }); }); ``` -------------------------------- ### Convert TEI table structures to HTML tables Source: https://context7.com/teic/ceteicean/llms.txt Demonstrates a custom behavior to map TEI table elements (tei-table, tei-row, tei-cell) into standard HTML table elements for browser rendering. ```javascript ct.addBehaviors({ "tei": { "table": function(elt) { const doc = elt.ownerDocument; const table = doc.createElement("table"); table.innerHTML = elt.innerHTML; // Convert head to caption if (table.firstElementChild?.localName === "tei-head") { const head = table.firstElementChild; head.remove(); const caption = doc.createElement("caption"); caption.innerHTML = head.innerHTML; table.appendChild(caption); } // Convert rows for (let row of Array.from(table.querySelectorAll("tei-row"))) { const tr = doc.createElement("tr"); tr.innerHTML = row.innerHTML; for (let attr of Array.from(row.attributes)) { tr.setAttribute(attr.name, attr.value); } row.parentElement.replaceChild(tr, row); } // Convert cells for (let cell of Array.from(table.querySelectorAll("tei-cell"))) { const td = doc.createElement("td"); if (cell.hasAttribute("cols")) { td.setAttribute("colspan", cell.getAttribute("cols")); } td.innerHTML = cell.innerHTML; for (let attr of Array.from(cell.attributes)) { td.setAttribute(attr.name, attr.value); } cell.parentElement.replaceChild(td, cell); } this.hideContent(elt, true); elt.appendChild(table); } } }); ``` -------------------------------- ### domToHTML5(XML_dom, callback, perElementFn) Source: https://context7.com/teic/ceteicean/llms.txt Converts an XML DOM object into HTML5 Custom Elements. Ideal for server-side processing or when working with pre-parsed XML documents. ```APIDOC ## domToHTML5(XML_dom, callback, perElementFn) Converts an XML DOM object into HTML5 Custom Elements. Ideal for server-side processing or when working with pre-parsed XML documents. ### Browser Usage with DOMParser ```javascript const ct = new CETEI(); const parser = new DOMParser(); const teiString = ` Sample Document

Published digitally

Born digital

This is a sample paragraph.

`; const xmlDoc = parser.parseFromString(teiString, "text/xml"); ct.domToHTML5(xmlDoc, function(data) { document.getElementById("TEI").appendChild(data); }); ``` ### Node.js Usage with JSDOM ```javascript import { JSDOM } from 'jsdom'; import CETEI from 'CETEIcean'; import fs from 'fs'; const tei = fs.readFileSync('document.xml', 'utf8'); const jdom = new JSDOM(tei, {contentType: 'text/xml'}); const teiDoc = jdom.window.document; const ct = new CETEI({documentObject: teiDoc}); const processedTEI = ct.domToHTML5(teiDoc); // Query the processed document const paragraphs = processedTEI.querySelectorAll("tei-p"); console.log(`Found ${paragraphs.length} paragraphs`); ``` ``` -------------------------------- ### Process and Render XML File with CETEI.js Source: https://github.com/teic/ceteicean/blob/master/tutorial_en/example/index.html Uses a CETEI.js instance to fetch and convert an XML file ('../fpn-washington.xml') into HTML5. The resulting HTML is then appended to the document's body. This is the final step in rendering the XML content on a web page. ```javascript c.getHTML5('../fpn-washington.xml', function(data){ document.getElementsByTagName('body')[0].appendChild(data); }); ``` -------------------------------- ### Convert TEI to HTML5 using CETEIcean with Promises (JavaScript) Source: https://github.com/teic/ceteicean/blob/master/test/simpleTest.html This snippet demonstrates an alternative method using Promises to convert TEI XML to HTML5 with the CETEIcean library. The converted HTML is appended to the 'TEI' element. This approach also requires modern browser support. ```javascript // Alternatively, use then() (new CETEI).getHTML5('testTEI.xml').then(function(data){ document.getElementById("TEI").appendChild(data); }); ``` -------------------------------- ### Process Pre-rendered CETEIcean Elements (JavaScript) Source: https://context7.com/teic/ceteicean/llms.txt The `processPage` method is designed to process CETEIcean Custom Elements that are already present in an HTML document, typically after server-side rendering. It applies defined behaviors to these existing elements, making it suitable for scenarios where initial DOM manipulation is handled externally. ```javascript // In HTML: elements are already converted (e.g., via XSLT) // Content here const ct = new CETEI(); ct.addBehaviors({ "tei": { "term": ["", ""] } }); ct.processPage(); // Applies behaviors to existing elements ``` -------------------------------- ### Convert TEI to HTML5 using CETEIcean (JavaScript) Source: https://github.com/teic/ceteicean/blob/master/test/simpleTest.html This snippet shows how to use the CETEIcean library to fetch a TEI XML file and convert it into HTML5. It appends the resulting HTML to a div with the ID 'TEI'. This method uses a callback function. It is not compatible with older browsers like Internet Explorer. ```javascript var CETEIcean = new CETEI(); CETEIcean.getHTML5('testTEI.xml', function(data) { document.getElementById("TEI").innerHTML = ""; document.getElementById("TEI").appendChild(data); }); ``` -------------------------------- ### Add Custom TEI Rendering Behaviors (JavaScript) Source: https://context7.com/teic/ceteicean/llms.txt The `addBehaviors` method allows for the addition of multiple custom rendering behaviors for TEI elements. Behaviors can be simple string replacements, functions returning DOM elements, or conditional mappings based on CSS selectors. This method is essential for customizing how TEI elements are displayed in HTML. ```javascript const ct = new CETEI(); ct.addBehaviors({ "tei": { // Simple wrapper: inserts before/after content "add": ["`", "´"], // Function behavior: returns new element "term": function(elt) { const b = document.createElement("b"); b.innerHTML = elt.innerHTML; return b; }, // Template with attribute substitution "ptr": ["$@target"], // Conditional behavior based on CSS selector "supplied": [ ["[reason=lost]", ["[", "]"]], ["[reason=omitted]", ["<", ">"]] ], // Complex function with DOM manipulation "graphic": function(elt) { const img = document.createElement("img"); img.src = this.rw(elt.getAttribute("url")); if (elt.hasAttribute("width")) { img.setAttribute("width", elt.getAttribute("width")); } if (elt.hasAttribute("height")) { img.setAttribute("height", elt.getAttribute("height")); } return img; }, // Header level detection "head": function(e) { const level = Number.parseInt(e.getAttribute("data-level")) || 1; const result = document.createElement("h" + (level < 6 ? level : 6)); for (let n of Array.from(e.childNodes)) { result.appendChild(n.cloneNode(true)); } return result; } } }); ct.getHTML5('document.xml', function(data) { document.getElementById("TEI").appendChild(data); }); ``` -------------------------------- ### Configure Default Link Behavior for TEI Ptr Element in JavaScript Source: https://github.com/teic/ceteicean/wiki/Anatomy-of-a-behaviors-object Sets up a default behavior for the TEI `` element to render as an HTML `` tag. The `@target` attribute of the `` is used for both the link's text and its `href` attribute. The `rw()` function is used to rewrite relative URLs for the `href`. ```javascript "ptr": ["$@target"] ``` -------------------------------- ### Render TEI headings as HTML headers Source: https://github.com/teic/ceteicean/wiki/Behaviors-Cookbook Converts TEI head elements into appropriate HTML heading tags (h1-h6) based on the data-level attribute. It clones child nodes to preserve content structure. ```javascript "head": function(e) { let level = Number.parseInt(e.getAttribute("data-level")); let result = document.createElement("h" + (level < 6 ? level : 6)); for (let n of Array.from(e.childNodes)) { result.appendChild(n.cloneNode()); } return result; } ``` -------------------------------- ### Process XML and Append HTML5 to DOM (JavaScript) Source: https://github.com/teic/ceteicean/blob/master/tutorial_es/example/index.html This JavaScript snippet utilizes the CETEI object to process an XML file ('../Ruy_Diaz-La_Argentina_Manuscrita.xml') and convert it into HTML5. The resulting HTML5 data is then appended as a child to the body element of the current document. ```javascript c.getHTML5('../Ruy_Diaz-La_Argentina_Manuscrita.xml', function(data){ document.getElementsByTagName('body')[0].appendChild(data); }); ``` -------------------------------- ### Set Base URL for Link Rewriting (JavaScript) Source: https://context7.com/teic/ceteicean/llms.txt The `setBaseUrl` method configures the base URL used for resolving relative links within TEI elements such as ``, ``, and ``. This ensures that all relative URLs in the source document are correctly interpreted against a specified base path. ```javascript const ct = new CETEI(); ct.setBaseUrl("https://example.com/tei-sources/"); ct.getHTML5('document.xml', function(data) { // All relative URLs in , , etc. // will be resolved against the base URL document.getElementById("TEI").appendChild(data); }); ``` -------------------------------- ### Transform persName tags with ref attributes into links Source: https://github.com/teic/ceteicean/wiki/Behaviors-Cookbook Uses a configuration pattern to wrap persName elements containing a ref attribute with an HTML anchor tag. Elements without the attribute are ignored. ```javascript "persName": [ ["[ref]", ["",""]] ] ``` -------------------------------- ### Convert TEI tables to HTML tables Source: https://github.com/teic/ceteicean/wiki/Behaviors-Cookbook Replaces custom TEI table elements (row and cell) with standard HTML table elements. It handles table captions and maps attributes from TEI elements to their HTML counterparts. ```javascript "table": function(elt) { let table = document.createElement("table"); table.innerHTML = elt.innerHTML; if (table.firstElementChild.localName == "tei-head") { let head = table.firstElementChild; head.remove(); let caption = document.createElement("caption"); caption.innerHTML = head.innerHTML; table.appendChild(caption); } for (let row of Array.from(table.querySelectorAll("tei-row"))) { let tr = document.createElement("tr"); tr.innerHTML = row.innerHTML; for (let attr of Array.from(row.attributes)) { tr.setAttribute(attr.name, attr.value); } row.parentElement.replaceChild(tr, row); } for (let cell of Array.from(table.querySelectorAll("tei-cell"))) { let td = document.createElement("td"); if (cell.hasAttribute("cols")) { td.setAttribute("colspan", cell.getAttribute("cols")); } td.innerHTML = cell.innerHTML; for (let attr of Array.from(cell.attributes)) { td.setAttribute(attr.name, attr.value); } cell.parentElement.replaceChild(td, cell); } this.hideContent(elt, true); elt.appendChild(table); } ``` -------------------------------- ### Convert inline TEI notes to endnotes Source: https://github.com/teic/ceteicean/wiki/Behaviors-Cookbook Transforms inline notes into numbered endnotes. It creates a reference link in the text and appends the full note content to an ordered list at the end of the document. ```javascript "note": function(elt){ if (!this.noteIndex){ this["noteIndex"] = 1; } else { this.noteIndex++; } let id = "note" + this.noteIndex; let link = document.createElement("a"); link.setAttribute("id", "src" + id); link.setAttribute("href", "#" + id); link.innerHTML = this.noteIndex; let content = document.createElement("sup"); content.appendChild(link); let notes = this.dom.querySelector("ol.notes"); if (!notes) { notes = document.createElement("ol"); notes.setAttribute("class", "notes"); this.dom.appendChild(notes); } let note = document.createElement("li"); note.id = id; note.innerHTML = "^ " + elt.innerHTML notes.appendChild(note); return content; } ``` -------------------------------- ### Add Custom Behaviors to CETEIcean (JavaScript) Source: https://github.com/teic/ceteicean/blob/master/test/addBehaviorTest.html This snippet demonstrates how to add custom behaviors to CETEIcean using JavaScript. It defines handlers for 'ptr' and 'term' elements within the 'tei' namespace. The 'ptr' handler creates a hyperlink, while the 'term' handler wraps the element's content in a bold tag. This functionality requires the CETEIcean library. ```javascript var c = new CETEI(); c.addBehaviors({"tei":{ "ptr": function(elt) { var link = document.createElement("a"); link.innerHTML = elt.getAttribute("target").replace(/https?:\/\/([^\/]+)\/.* /, "$1"); link.href = elt.getAttribute("target"); return link; }, "term": function(elt) { var b = document.createElement("b"); b.innerHTML = elt.innerHTML; return b; }, "add":["`","´"] }}}); ```