0.2.3 • Published 6 years ago

html-schema-parser v0.2.3

Weekly downloads
12
License
-
Repository
-
Last release
6 years ago

html-schema-parser

Document

A HTML parser based on schema Reference source code https://github.com/caolan/pithy

Usage

Node Usage

npm install --save http-schema-parser
const HtmlSchemaParser = require('http-schema-parser')
const schema = [
  {
    tag: 'div',
    attribute: {
      style: 'background:#f0f0f0;'
    },
    content: [
      {
        tag: 'span',
        content: ['hello']
      },
      {
        tag: 'span',
        content: ['world']
      },
      {
        tag: 'ul',
        content: [
          {
            tag: 'li',
            content: ['item 001']
          },
          {
            tag: 'li',
            content: ['item 002']
          }
        ]
      }
    ],
  }
]

let scheamParser = new HtmlSchemaParser(schema);
let htmlStr = scheamParser.getHtml();

const $body = document.querySelector('body');
$body.innerHTML = htmlStr;

Browser Usage

<script src="html-schema-parser.js"></script>
<script>
  // Code ...
  var HtmlSchemaParser = window.HtmlSchemaParser;
  var scheamParser = new HtmlSchemaParser(schema);
  var html = scheamParser.getHtml();

</script>

Instructions

schema propschema typeexample
tagString{"tag":"div"}
attributeObject{"attribute":{ "style":"background:#f0f0f0" }}
contentArray{ "content": ['hello', 'world', {"tag":"span", conent:['001']}, {"tag":"span", conent:['002']}] }

中文说明

基于schema格式的HTML解析器 参考源码自 https://github.com/caolan/pithy

使用说明

Node 使用方式

npm install --save http-schema-parser
const HtmlSchemaParser = require('http-schema-parser')
const schema = [
  {
    tag: 'div',
    attribute: {
      style: 'background:#f0f0f0;'
    },
    content: [
      {
        tag: 'span',
        content: ['hello']
      },
      {
        tag: 'span',
        content: ['world']
      },
      {
        tag: 'ul',
        content: [
          {
            tag: 'li',
            content: ['item 001']
          },
          {
            tag: 'li',
            content: ['item 002']
          }
        ]
      }
    ],
  }
]

let scheamParser = new HtmlSchemaParser(schema);
let htmlStr = scheamParser.getHtml();

const $body = document.querySelector('body');
$body.innerHTML = htmlStr;

浏览器使用方式

<script src="html-schema-parser.js"></script>
<script>
  // Code ...
  var HtmlSchemaParser = window.HtmlSchemaParser;
  var scheamParser = new HtmlSchemaParser(schema);
  var html = scheamParser.getHtml();

</script>

说明

schema属性schema类型例子
tagString{"tag":"div"}
attributeObject{"attribute":{ "style":"background:#f0f0f0" }}
contentArray{ "content": ['hello', 'world', {"tag":"span", conent:['001']}, {"tag":"span", conent:['002']}] }
0.2.3

6 years ago

0.2.2

6 years ago

0.2.1

6 years ago

0.2.0

6 years ago