ziba-js/ziba.js

134 lines
3.6 KiB
JavaScript
Raw Normal View History

2023-11-15 18:09:31 +00:00
/*
Copyright (c) 2023 Charles Iliya Krempeaux :: http://changelog.ca/
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
export function transform(rootElement) {
2023-11-16 16:26:46 +00:00
log("[transform] begin")
2023-11-16 00:05:06 +00:00
2023-11-15 18:09:31 +00:00
if (undefined === rootElement) {
2023-11-16 16:26:46 +00:00
log("[transform] root-elelemt is undefined")
log("[transform] end")
2023-11-15 18:09:31 +00:00
return
}
transform_link(rootElement)
2023-11-16 00:05:06 +00:00
2023-11-16 16:26:46 +00:00
log("[transform] end")
2023-11-15 18:09:31 +00:00
}
function transform_link(rootElement) {
2023-11-16 16:26:46 +00:00
log("[transform_link] begin")
2023-11-16 00:05:06 +00:00
2023-11-15 18:09:31 +00:00
if (undefined === rootElement) {
2023-11-16 16:26:46 +00:00
logerror("[transform_link] root-elelemt is undefined")
log("[transform_link] end")
2023-11-15 18:09:31 +00:00
return
}
const tagName = "ziba-link"
const elements = rootElement.getElementsByTagName(tagName)
if (!elements) {
2023-11-16 16:26:46 +00:00
logerror("[transform_link] no elements")
log("[transform_link] rootElement.innerHTML=", rootElement.innerHTML)
log("[transform_link] end")
2023-11-15 18:09:31 +00:00
return
}
2023-11-16 00:05:06 +00:00
const length = elements.length
2023-11-16 16:26:46 +00:00
log("[transform_link] found", length, tagName)
2023-11-15 18:09:31 +00:00
2023-11-15 19:30:50 +00:00
// Because we are doing a replaceWith() on the HTMLCollection,
// we need to iterate through it backwards, because the HTMLCollection
// will change size and what is in it each time we do that.
//
//@TODO: Is there a better way of dealing with this?
for (let i = length-1; i >= 0; i--) {
const element = elements[i]
2023-11-15 18:09:31 +00:00
const anchor = document.createElement("a")
anchor.innerHTML = element.innerHTML
2023-11-16 16:26:46 +00:00
let ref = element.innerText
2023-11-17 02:43:26 +00:00
const elementName = element.getAttribute("name")
if (elementName) {
ref = elementName
2023-11-15 18:09:31 +00:00
}
2023-11-16 16:26:46 +00:00
const transform = element.getAttribute("transform")
if (transform) {
switch (transform) {
case "lowercase":
ref = ref.toLowerCase()
break;
case "lowerllamacase":
ref = ref.toLowerCase().replaceAll(" ", "")
break;
2023-11-16 16:26:46 +00:00
case "lowersnakecase":
ref = ref.toLowerCase().replaceAll(" ", "_")
break;
case "lowerslugcase":
ref = ref.toLowerCase().replaceAll(" ", "-")
break;
2023-11-16 16:26:46 +00:00
case "uppercase":
ref = ref.toUpperCase()
break;
case "upperllamacase":
ref = ref.toUpperCase().replaceAll(" ", "")
break;
2023-11-16 16:26:46 +00:00
case "uppersnakecase":
ref = ref.toUpperCase().replaceAll(" ", "_")
break;
case "upperslugcase":
ref = ref.toUpperCase().replaceAll(" ", "-")
break;
2023-11-16 16:26:46 +00:00
default:
2023-11-17 17:37:10 +00:00
logerror("unknown transform:", transform)
2023-11-16 16:26:46 +00:00
// Nothing here.
}
}
2023-11-17 06:23:28 +00:00
let dir = element.getAttribute("dir")
let href = "./"
if ('string' === typeof dir && "" !== dir) {
href += dir
href += "/"
}
href += ref
2023-11-15 18:09:31 +00:00
anchor.setAttribute("href", href)
element.replaceWith(anchor)
2023-11-15 19:30:50 +00:00
}
2023-11-16 00:05:06 +00:00
2023-11-16 16:26:46 +00:00
log("[transform_link] end")
}
function log(...args) {
args.unshift("[ziba]");
console.log(...args);
}
function logerror(...args) {
args.unshift("[ziba] ERROR:");
console.error(...args);
2023-11-15 18:09:31 +00:00
}