vtm/quelldatein/helfer/xml.ts

157 lines
3.2 KiB
TypeScript
Raw Normal View History

2018-03-28 11:19:46 +02:00
/*
* Verrückte Turing-Maschinen A turing complete game
* Copyright (C) 2016 Christian Fraß <vidofnir@folksprak.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
module lib_xml
{
/**
* @author kcf <vidofnir@folksprak.org>
*/
2018-03-28 13:59:29 +02:00
function indent
2018-03-28 11:19:46 +02:00
(
2018-03-28 13:59:29 +02:00
depth : int,
2018-03-28 11:19:46 +02:00
zeichen : string = "\t"
)
: string
{
return (
2018-03-28 13:59:29 +02:00
(depth === 0)
2018-03-28 11:19:46 +02:00
? ""
2018-03-28 13:59:29 +02:00
: (zeichen + indent(depth-1))
2018-03-28 11:19:46 +02:00
);
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
2018-03-28 13:59:29 +02:00
export type type_node = lib_call.type_complex<any>;
2018-03-28 11:19:46 +02:00
/**
* @author kcf <vidofnir@folksprak.org>
*/
2018-03-28 13:59:29 +02:00
export function create_text
2018-03-28 11:19:46 +02:00
(
2018-03-28 13:59:29 +02:00
content : string
2018-03-28 11:19:46 +02:00
)
2018-03-28 13:59:29 +02:00
: type_node
2018-03-28 11:19:46 +02:00
{
return (
2018-03-28 13:59:29 +02:00
lib_call.wrap
2018-03-28 11:19:46 +02:00
(
"text",
{
2018-03-28 13:59:29 +02:00
"content": content
2018-03-28 11:19:46 +02:00
}
)
);
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
2018-03-28 13:59:29 +02:00
export function create_normal
2018-03-28 11:19:46 +02:00
(
name : string,
2018-03-28 13:59:29 +02:00
attributes : {[key : string] : string} = {},
children : Array<type_node> = []
2018-03-28 11:19:46 +02:00
)
2018-03-28 13:59:29 +02:00
: type_node
2018-03-28 11:19:46 +02:00
{
return (
2018-03-28 13:59:29 +02:00
lib_call.wrap
2018-03-28 11:19:46 +02:00
(
"normal",
{
"name": name,
2018-03-28 13:59:29 +02:00
"attributes": attributes,
"children": children
2018-03-28 11:19:46 +02:00
}
)
);
}
/**
* @author kcf <vidofnir@folksprak.org>
*/
2018-03-28 13:59:29 +02:00
export function view
2018-03-28 11:19:46 +02:00
(
2018-03-28 13:59:29 +02:00
node : type_node,
depth : int = 0
2018-03-28 11:19:46 +02:00
)
: string
{
return (
2018-03-28 13:59:29 +02:00
lib_call.distinguish<any>
2018-03-28 11:19:46 +02:00
(
2018-03-28 13:59:29 +02:00
node,
2018-03-28 11:19:46 +02:00
{
2018-03-28 13:59:29 +02:00
"text": ({"content": content}) =>
2018-03-28 11:19:46 +02:00
{
2018-03-28 13:59:29 +02:00
return content;
2018-03-28 11:19:46 +02:00
}
,
2018-03-28 13:59:29 +02:00
"normal": ({"name": name, "attributes": attributes, "children": children}) =>
2018-03-28 11:19:46 +02:00
{
let str : string = "";
2018-03-28 13:59:29 +02:00
// begin
2018-03-28 11:19:46 +02:00
{
2018-03-28 13:59:29 +02:00
let str_begin : string = "";
str_begin += name;
// attributes
2018-03-28 11:19:46 +02:00
{
2018-03-28 13:59:29 +02:00
let str_attributes : string = "";
Object.keys(attributes).forEach
2018-03-28 11:19:46 +02:00
(
2018-03-28 13:59:29 +02:00
key =>
2018-03-28 11:19:46 +02:00
{
2018-03-28 13:59:29 +02:00
let value : string = attributes[key];
str_attributes += (" " + key + "=" + ("\"" + value + "\""));
2018-03-28 11:19:46 +02:00
}
)
;
2018-03-28 13:59:29 +02:00
str_begin += str_attributes;
2018-03-28 11:19:46 +02:00
}
2018-03-28 13:59:29 +02:00
str_begin = (indent(depth) + "<" + str_begin + ">" + "\n");
str += str_begin;
2018-03-28 11:19:46 +02:00
}
2018-03-28 13:59:29 +02:00
// children
2018-03-28 11:19:46 +02:00
{
2018-03-28 13:59:29 +02:00
children.forEach(child => (str += view(child, depth+1)));
2018-03-28 11:19:46 +02:00
}
2018-03-28 13:59:29 +02:00
// end
2018-03-28 11:19:46 +02:00
{
2018-03-28 13:59:29 +02:00
let str_end : string = "";
str_end += name;
str_end = (indent(depth) + "<" + "/" + str_end + ">" + "\n");
str += str_end;
2018-03-28 11:19:46 +02:00
}
return str;
}
,
}
)
);
}
}