hi-xml2html
Advanced tools
Comparing version 1.1.0 to 1.2.0
@@ -1,5 +0,7 @@ | ||
import { ISettings } from "./types"; | ||
import BaseTag from './base-tag'; | ||
export { BaseTag }; | ||
declare var _default: (xmlString: string, settings?: ISettings) => Promise<string>; | ||
import { ISettings, IState } from "./types"; | ||
import HtmlTag from './tags/html'; | ||
import JsxTag from './tags/jsx'; | ||
import EmptyTag from './tags/empty'; | ||
export { EmptyTag, HtmlTag, JsxTag }; | ||
declare var _default: (xmlString: string, settings?: ISettings) => Promise<IState>; | ||
export default _default; |
@@ -8,4 +8,8 @@ "use strict"; | ||
const parse_text_1 = require("./parse-text"); | ||
const base_tag_1 = require("./base-tag"); | ||
exports.BaseTag = base_tag_1.default; | ||
const html_1 = require("./tags/html"); | ||
exports.HtmlTag = html_1.default; | ||
const jsx_1 = require("./tags/jsx"); | ||
exports.JsxTag = jsx_1.default; | ||
const empty_1 = require("./tags/empty"); | ||
exports.EmptyTag = empty_1.default; | ||
exports.default = (xmlString, settings = {}) => new Promise((resolve, reject) => { | ||
@@ -18,4 +22,4 @@ const state = new state_1.default(settings); | ||
parser.onerror = (e) => reject(e); | ||
parser.onend = () => resolve(state.wrapOutput()); | ||
parser.onend = () => resolve(state); | ||
parser.write(xmlString).close(); | ||
}); |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const base_tag_1 = require("./base-tag"); | ||
exports.default = (state) => (node) => { | ||
@@ -9,3 +8,3 @@ if (state.startFromTag === node.name) | ||
state.tags[node.name] : | ||
base_tag_1.default; | ||
state.GenericTag; | ||
const tag = new Tag(node, state); | ||
@@ -12,0 +11,0 @@ const open = tag.open(); |
import OpenTags from './open-tags'; | ||
import PreviousNodes from './previous-nodes'; | ||
import { ISettings, IState } from "../types"; | ||
import { ISettings, IState, TagClasses } from "../types"; | ||
declare class State implements IState { | ||
private componentsPath; | ||
private output; | ||
jsx: boolean; | ||
output: string; | ||
GenericTag: any; | ||
openTags: OpenTags; | ||
previousNodes: PreviousNodes; | ||
startFromTag: any; | ||
tagClass: TagClasses; | ||
tags: any; | ||
@@ -17,5 +18,3 @@ tagsToSkip: any; | ||
appendHtml(str: any): void; | ||
wrapOutput(): string; | ||
private wrapJsx(); | ||
} | ||
export default State; |
@@ -5,14 +5,15 @@ "use strict"; | ||
const previous_nodes_1 = require("./previous-nodes"); | ||
const jsx_1 = require("../tags/jsx"); | ||
const html_1 = require("../tags/html"); | ||
const empty_1 = require("../tags/empty"); | ||
class State { | ||
constructor(settings) { | ||
this.output = ''; | ||
this.jsx = false; | ||
this.openTags = new open_tags_1.default(); | ||
this.previousNodes = new previous_nodes_1.default(); | ||
this.tagClass = 'html'; | ||
this.usedTags = new Set(); | ||
this.writeToOutput = false; | ||
let { componentsPath, jsx, startFromTag, tags, tagsToSkip } = settings; | ||
let { componentsPath, startFromTag, tagClass, tags, tagsToSkip } = settings; | ||
this.componentsPath = (componentsPath == null) ? 'components' : componentsPath; | ||
if (jsx) | ||
this.jsx = true; | ||
this.startFromTag = startFromTag; | ||
@@ -24,2 +25,7 @@ if (startFromTag == null) | ||
this.appendHtml(false); | ||
if (tagClass != null && tagClass.length) | ||
this.tagClass = tagClass; | ||
this.GenericTag = this.tagClass === 'html' ? | ||
html_1.default : | ||
this.tagClass === 'jsx' ? jsx_1.default : empty_1.default; | ||
} | ||
@@ -30,17 +36,3 @@ appendHtml(str) { | ||
} | ||
wrapOutput() { | ||
return (this.jsx) ? this.wrapJsx() : this.output; | ||
} | ||
wrapJsx() { | ||
const tags = [...this.usedTags].join(', '); | ||
return (`import * as React from 'react'; | ||
import { ${tags} } from '${this.componentsPath}'; | ||
export default () => ( | ||
<div className="wrapper"> | ||
${this.output} | ||
</div> | ||
);`); | ||
} | ||
} | ||
exports.default = State; |
import { Tag } from "sax"; | ||
export interface IBaseTag { | ||
state: IState; | ||
} | ||
export interface ICustomTag extends IBaseTag { | ||
close(): string; | ||
open(): string; | ||
} | ||
export declare type TagClasses = 'html' | 'jsx' | 'empty'; | ||
export interface IState { | ||
jsx: boolean; | ||
openTags: any; | ||
previousNodes: any; | ||
startFromTag: string; | ||
tagClass: TagClasses; | ||
tags: any; | ||
@@ -17,3 +20,2 @@ tagsToSkip: string[]; | ||
appendHtml(str: string): void; | ||
wrapOutput(): string; | ||
[prop: string]: any; | ||
@@ -23,4 +25,4 @@ } | ||
componentsPath?: string; | ||
jsx?: boolean; | ||
startFromTag?: string; | ||
tagClass?: TagClasses; | ||
tags?: Object; | ||
@@ -27,0 +29,0 @@ tagsToSkip?: any[]; |
@@ -0,1 +1,4 @@ | ||
### v1.2.0 (2017/4/3 16:42) | ||
* Add EmptyTag | ||
### v1.1.0 (2017/3/31 15:5) | ||
@@ -2,0 +5,0 @@ |
{ | ||
"name": "hi-xml2html", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "", | ||
@@ -9,2 +9,3 @@ "main": "build/index.js", | ||
"bump": "hi-bump", | ||
"test": "node test/index.js", | ||
"watch": "tsc -w" | ||
@@ -11,0 +12,0 @@ }, |
@@ -6,9 +6,11 @@ import * as sax from "sax"; | ||
import parseText from './parse-text'; | ||
import {ISettings} from "./types"; | ||
import {ISettings, IState} from "./types"; | ||
import BaseTag from './base-tag'; | ||
export { BaseTag } ; | ||
import HtmlTag from './tags/html'; | ||
import JsxTag from './tags/jsx'; | ||
import EmptyTag from './tags/empty'; | ||
export { EmptyTag, HtmlTag, JsxTag } ; | ||
export default (xmlString: string, settings: ISettings = {}) => | ||
new Promise<string>((resolve, reject) => { | ||
new Promise<IState>((resolve, reject) => { | ||
const state = new State(settings); | ||
@@ -20,4 +22,4 @@ const parser = sax.parser(true, {}); | ||
parser.onerror = (e) => reject(e); | ||
parser.onend = () => resolve(state.wrapOutput()); | ||
parser.onend = () => resolve(state); | ||
parser.write(xmlString).close(); | ||
}); |
@@ -1,2 +0,1 @@ | ||
import BaseTag from './base-tag'; | ||
import {Tag} from "sax"; | ||
@@ -6,5 +5,6 @@ | ||
if (state.startFromTag === node.name) state.writeToOutput = true; | ||
const Tag = Object.keys(state.tags).indexOf(node.name) > -1 ? | ||
state.tags[node.name] : | ||
BaseTag; | ||
state.GenericTag; | ||
const tag = new Tag(node, state); | ||
@@ -11,0 +11,0 @@ const open = tag.open(); |
import OpenTags from './open-tags'; | ||
import PreviousNodes from './previous-nodes'; | ||
import {ISettings, IState} from "../types"; | ||
import {ICustomTag, ISettings, IState, TagClasses} from "../types"; | ||
import JsxTag from "../tags/jsx"; | ||
import HtmlTag from "../tags/html"; | ||
import EmptyTag from "../tags/empty"; | ||
class State implements IState { | ||
private componentsPath: string; | ||
private output: string = ''; | ||
public output: string = ''; | ||
public jsx = false; | ||
public GenericTag; | ||
public openTags = new OpenTags(); | ||
public previousNodes = new PreviousNodes(); | ||
public startFromTag; | ||
public tagClass: TagClasses = 'html'; | ||
public tags; | ||
@@ -19,5 +23,4 @@ public tagsToSkip; | ||
constructor(settings: ISettings) { | ||
let { componentsPath, jsx, startFromTag, tags, tagsToSkip } = settings; | ||
let { componentsPath, startFromTag, tagClass, tags, tagsToSkip } = settings; | ||
this.componentsPath = (componentsPath == null) ? 'components' : componentsPath; | ||
if (jsx) this.jsx = true; | ||
this.startFromTag = startFromTag; | ||
@@ -27,3 +30,7 @@ if (startFromTag == null) this.writeToOutput = true; | ||
this.tagsToSkip = (tagsToSkip == null) ? [] : tagsToSkip; | ||
this.appendHtml(false) | ||
this.appendHtml(false); | ||
if (tagClass != null && tagClass.length) this.tagClass = tagClass; | ||
this.GenericTag = this.tagClass === 'html' ? | ||
HtmlTag : | ||
this.tagClass === 'jsx' ? JsxTag : EmptyTag; | ||
} | ||
@@ -34,24 +41,4 @@ | ||
} | ||
public wrapOutput() { | ||
return (this.jsx) ? this.wrapJsx() : this.output; | ||
} | ||
private wrapJsx(): string { | ||
const tags = [...this.usedTags].join(', '); | ||
return ( | ||
// Do not indent! | ||
`import * as React from 'react'; | ||
import { ${tags} } from '${this.componentsPath}'; | ||
export default () => ( | ||
<div className="wrapper"> | ||
${this.output} | ||
</div> | ||
);` | ||
); | ||
} | ||
} | ||
export default State; |
@@ -5,2 +5,5 @@ import {Tag} from "sax"; | ||
state: IState; | ||
} | ||
export interface ICustomTag extends IBaseTag { | ||
close(): string; | ||
@@ -10,8 +13,11 @@ open(): string; | ||
export type TagClasses = 'html' | 'jsx' | 'empty'; | ||
export interface IState { | ||
jsx: boolean; | ||
openTags; | ||
previousNodes; | ||
startFromTag: string; | ||
tagClass: TagClasses; | ||
tags; | ||
// ToDo make more flexibel: [{name: 'hi', rend: 'super'}] | ||
tagsToSkip: string[]; | ||
@@ -21,3 +27,2 @@ usedTags: Set<string>; | ||
appendHtml(str: string): void; | ||
wrapOutput(): string; | ||
[prop: string]: any; | ||
@@ -30,5 +35,2 @@ } | ||
// Output JSX instead of HTML. | ||
jsx?: boolean; | ||
// When the parser encouters this tag name, the parser starts writing | ||
@@ -38,2 +40,3 @@ // to this.output. The tag name should be a unique tag (like <body>). | ||
tagClass?: TagClasses; | ||
// Maps a tag name (key) to a tag class (value). The tag class may extend | ||
@@ -40,0 +43,0 @@ // BaseTag. If a tag is not in the map, BaseTag is used to generate output. |
@@ -8,2 +8,3 @@ const fs = require('fs'); | ||
const jsxOutput = `${path}test.jsx`; | ||
const emptyOutput = `${path}test.empty`; | ||
@@ -13,14 +14,19 @@ const main = async () => { | ||
const html = await xml2html(xmlString, { | ||
const htmlState = await xml2html(xmlString, { | ||
startFromTag: 'text', | ||
}); | ||
fs.writeFileSync(htmlOutput, html, 'utf-8'); | ||
fs.writeFileSync(htmlOutput, htmlState.output, 'utf-8'); | ||
const jsx = await xml2html(xmlString, { | ||
const jsxState = await xml2html(xmlString, { | ||
componentsPath: 'client/components/entry', | ||
jsx: true, | ||
tagClass: 'jsx', | ||
startFromTag: 'body', | ||
}); | ||
fs.writeFileSync(jsxOutput, jsx, 'utf-8'); | ||
fs.writeFileSync(jsxOutput, jsxState.output, 'utf-8'); | ||
const emptyState = await xml2html(xmlString, { | ||
tagClass: 'empty', | ||
startFromTag: 'body', | ||
}); | ||
fs.writeFileSync(emptyOutput, emptyState.output, 'utf-8'); | ||
}; | ||
@@ -27,0 +33,0 @@ |
@@ -1,8 +0,3 @@ | ||
import * as React from 'react'; | ||
import { Body, Div, Pb, Address, AddrLine, MdPostmark, Ab, P, Lb, Choice, Unclear, Hi, Del, C, Add, Retrace, Gap, Space, Supplied, Note } from 'client/components/entry'; | ||
<Body> | ||
export default () => ( | ||
<div className="wrapper"> | ||
<Body> | ||
<Div> | ||
@@ -561,4 +556,2 @@ <Pb data-n="envelope-v" data-xml-id="env-v" data-facs="#zone-env-v"/> | ||
</Div> | ||
</Body> | ||
</div> | ||
); | ||
</Body> |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
132479
50
1291
0