@marrs/sxml 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENCE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 David Marrs
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,13 @@
1
+ S-Expression Markup Language (SXML)
2
+
3
+ This is a simple language for writing XML and HTML. It's for those of us who
4
+ would like to write HTML directly but find the language a little bit too
5
+ clunky and a little too easy to make mistakes with. SXML just makes it a
6
+ little bit nicer.
7
+
8
+ Minimal effort is made to guard against bad code and it is quite possible to
9
+ produce broken HTML. For convenience, angled brackets are escaped in positions
10
+ where that is likely to be the preferred option.
11
+
12
+ If you need to guard against script injection, you should run the HTML
13
+ generated from the SXML against a library dedicated to that purpose.
package/cli/index.js ADDED
@@ -0,0 +1,32 @@
1
+ #!/usr/bin/env node
2
+ import fs from 'fs'
3
+ import { StringDecoder } from 'string_decoder'
4
+ import { Buffer } from 'buffer';
5
+ import { init_parse_state, update_parse_state } from '../src/parser.js'
6
+ import await_init from '../src/init.js';
7
+
8
+ var idxFilename = (/node$/.test(process.argv[0]))? 2 : 1;
9
+ var filename = process.argv[idxFilename];
10
+ if (!filename) {
11
+ console.error("Please provide an SXML file");
12
+ process.exit();
13
+ }
14
+
15
+ await_init().then(function(data) {
16
+ var readStream = fs.createReadStream(filename);
17
+ var parseState, decoder;
18
+ decoder = new StringDecoder('utf8');
19
+ readStream.on('open', function() {
20
+ parseState = init_parse_state({
21
+ filename: filename,
22
+ extensions: data.extensions
23
+ });
24
+ });
25
+
26
+ readStream.on('data', function(chunk) {
27
+ var result = [];
28
+ var strChunk = decoder.write(chunk);
29
+ update_parse_state(strChunk, parseState, result);
30
+ process.stdout.write(Buffer.from(result.join('')));
31
+ });
32
+ });
package/lib/index.js ADDED
@@ -0,0 +1,3 @@
1
+ import { compile } from '../src/index.js';
2
+
3
+ export { compile }
@@ -0,0 +1,15 @@
1
+ import {
2
+ pre_html_chars,
3
+ pre_html_entity,
4
+ pre_hex_chars,
5
+ pre_hex_entity,
6
+ pre_qualified_ns,
7
+ } from '../src/processors/index.js';
8
+
9
+ export {
10
+ pre_html_chars as htmlChars,
11
+ pre_html_entity as htmlEntity,
12
+ pre_hex_chars as hexChars,
13
+ pre_hex_entity as hexEntity,
14
+ pre_qualified_ns as qualifiedNs,
15
+ };
package/package.json ADDED
@@ -0,0 +1,40 @@
1
+ {
2
+ "name": "@marrs/sxml",
3
+ "version": "0.3.0",
4
+ "description": "S-Expression Markup Language",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "git+https://github.com/marrs/sxml.git"
8
+ },
9
+ "exports": {
10
+ ".": "./lib/index.js",
11
+ "./preprocessors": "./lib/preprocessors.js"
12
+ },
13
+ "type": "module",
14
+ "files": [
15
+ "lib",
16
+ "src",
17
+ "cli",
18
+ "README.md",
19
+ "LICENCE"
20
+ ],
21
+ "scripts": {
22
+ "sxml": "./cli/index.js",
23
+ "test": "mocha"
24
+ },
25
+ "bin": {
26
+ "sxml": "cli/index.js"
27
+ },
28
+ "author": "David Marrs",
29
+ "license": "MIT",
30
+ "private": false,
31
+ "publishConfig": {
32
+ "access": "public"
33
+ },
34
+ "devDependencies": {
35
+ "chai": "^4.3.6",
36
+ "mocha": "^9.2.2",
37
+ "sinon": "^13.0.1",
38
+ "sinon-chai": "^3.7.0"
39
+ }
40
+ }
package/src/buffer.js ADDED
@@ -0,0 +1,72 @@
1
+ import { count_newlines } from './util.js'
2
+
3
+ function index_of_token_end(tkn) {
4
+ var matchEndOfToken = tkn.match(/(\)|\s)/);
5
+ return matchEndOfToken? matchEndOfToken.index : -1
6
+ }
7
+
8
+ export var Buffer_Trait = {
9
+ reset: function(idx) {
10
+ var start = this.cursor;
11
+ this.cursor = idx || 0;
12
+ if (this.cursor > this.str.length) {
13
+ this.cursor = this.str.length;
14
+ }
15
+ if (start < this.cursor) {
16
+ this.lineCount += count_newlines(this.str.substring(start, this.cursor));
17
+ }
18
+ this.substr = this.str.substring(this.cursor);
19
+ },
20
+
21
+ step: function(offset) {
22
+ if (void 0 === offset) {
23
+ offset = 1;
24
+ }
25
+ this.reset(this.cursor + offset);
26
+ },
27
+
28
+ skip_whitespace: function(x) {
29
+ x = (x === void 0)? Number.MAX_VALUE : x;
30
+
31
+ var nextNonWsChar = this.substr.match(/[^\s]/);
32
+ return nextNonWsChar?
33
+ this.step(Math.min(x, nextNonWsChar.index)):
34
+ this.reset(Math.min(x, this.str.length));
35
+ },
36
+
37
+ read_whitespace: function() {
38
+ var nextNonWsChar = this.substr.match(/[^\s]/);
39
+ if (nextNonWsChar) {
40
+ return this.read_to(nextNonWsChar.index);
41
+ }
42
+ return this.read_to_end();
43
+ },
44
+
45
+ read_to: function(offset) {
46
+ if (typeof offset === 'string') {
47
+ offset = this.substr.indexOf(offset);
48
+ if (offset < 0) {
49
+ offset = this.substr.length;
50
+ }
51
+ }
52
+ var output = this.substr.substring(0, offset);
53
+ this.step(offset);
54
+ return output;
55
+ },
56
+
57
+ read_to_end: function() {
58
+ var output = this.str.substring(this.cursor);
59
+ this.reset(this.str.length);
60
+ return output;
61
+ }
62
+ };
63
+
64
+ export var Sexp_Buffer_Trait = Object.create(Buffer_Trait);
65
+
66
+ Object.assign(Sexp_Buffer_Trait, {
67
+ read_token: function() {
68
+ var idxTokenEnd = index_of_token_end(this.substr);
69
+ return idxTokenEnd < 0?
70
+ this.read_to_end() : this.read_to(idxTokenEnd);
71
+ }
72
+ });
package/src/index.js ADDED
@@ -0,0 +1,31 @@
1
+ import { ReadStream, readFileSync } from 'fs';
2
+ import {
3
+ init_parse_state,
4
+ parse_chunk,
5
+ } from '../src/parser.js'
6
+
7
+ function is_string(value) {
8
+ return typeof value === 'string' || value instanceof String;
9
+ }
10
+
11
+ export function compile(source, options={}) {
12
+ // source can be one of:
13
+ // - fs.ReadStream
14
+ // - String.
15
+
16
+ var data = init_parse_state({
17
+ source,
18
+ isPrettyFormatting: options.pretty || false,
19
+ extensions: options.extensions || {},
20
+ });
21
+ var result = [];
22
+ if (source instanceof ReadStream) {
23
+ parse_chunk(readFileSync(source.path, 'utf8'), data, result);
24
+ return result.join('');
25
+ } else if (is_string(source)) {
26
+ parse_chunk(source, data, result);
27
+ return result.join('');
28
+ } else {
29
+ throw new Error("Incompatible type: Expected String or fs.ReadStream");
30
+ }
31
+ }
package/src/init.js ADDED
@@ -0,0 +1,5 @@
1
+ import { await_extensions } from './processors/index.js';
2
+
3
+ export default function await_init() {
4
+ return await_extensions();
5
+ }