Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. # sax js
  2. A sax-style parser for XML and HTML.
  3. Designed with [node](http://nodejs.org/) in mind, but should work fine in
  4. the browser or other CommonJS implementations.
  5. ## What This Is
  6. * A very simple tool to parse through an XML string.
  7. * A stepping stone to a streaming HTML parser.
  8. * A handy way to deal with RSS and other mostly-ok-but-kinda-broken XML
  9. docs.
  10. ## What This Is (probably) Not
  11. * An HTML Parser - That's a fine goal, but this isn't it. It's just
  12. XML.
  13. * A DOM Builder - You can use it to build an object model out of XML,
  14. but it doesn't do that out of the box.
  15. * XSLT - No DOM = no querying.
  16. * 100% Compliant with (some other SAX implementation) - Most SAX
  17. implementations are in Java and do a lot more than this does.
  18. * An XML Validator - It does a little validation when in strict mode, but
  19. not much.
  20. * A Schema-Aware XSD Thing - Schemas are an exercise in fetishistic
  21. masochism.
  22. * A DTD-aware Thing - Fetching DTDs is a much bigger job.
  23. ## Regarding `<!DOCTYPE`s and `<!ENTITY`s
  24. The parser will handle the basic XML entities in text nodes and attribute
  25. values: `&amp; &lt; &gt; &apos; &quot;`. It's possible to define additional
  26. entities in XML by putting them in the DTD. This parser doesn't do anything
  27. with that. If you want to listen to the `ondoctype` event, and then fetch
  28. the doctypes, and read the entities and add them to `parser.ENTITIES`, then
  29. be my guest.
  30. Unknown entities will fail in strict mode, and in loose mode, will pass
  31. through unmolested.
  32. ## Usage
  33. ```javascript
  34. var sax = require("./lib/sax"),
  35. strict = true, // set to false for html-mode
  36. parser = sax.parser(strict);
  37. parser.onerror = function (e) {
  38. // an error happened.
  39. };
  40. parser.ontext = function (t) {
  41. // got some text. t is the string of text.
  42. };
  43. parser.onopentag = function (node) {
  44. // opened a tag. node has "name" and "attributes"
  45. };
  46. parser.onattribute = function (attr) {
  47. // an attribute. attr has "name" and "value"
  48. };
  49. parser.onend = function () {
  50. // parser stream is done, and ready to have more stuff written to it.
  51. };
  52. parser.write('<xml>Hello, <who name="world">world</who>!</xml>').close();
  53. // stream usage
  54. // takes the same options as the parser
  55. var saxStream = require("sax").createStream(strict, options)
  56. saxStream.on("error", function (e) {
  57. // unhandled errors will throw, since this is a proper node
  58. // event emitter.
  59. console.error("error!", e)
  60. // clear the error
  61. this._parser.error = null
  62. this._parser.resume()
  63. })
  64. saxStream.on("opentag", function (node) {
  65. // same object as above
  66. })
  67. // pipe is supported, and it's readable/writable
  68. // same chunks coming in also go out.
  69. fs.createReadStream("file.xml")
  70. .pipe(saxStream)
  71. .pipe(fs.createWriteStream("file-copy.xml"))
  72. ```
  73. ## Arguments
  74. Pass the following arguments to the parser function. All are optional.
  75. `strict` - Boolean. Whether or not to be a jerk. Default: `false`.
  76. `opt` - Object bag of settings regarding string formatting. All default to `false`.
  77. Settings supported:
  78. * `trim` - Boolean. Whether or not to trim text and comment nodes.
  79. * `normalize` - Boolean. If true, then turn any whitespace into a single
  80. space.
  81. * `lowercase` - Boolean. If true, then lowercase tag names and attribute names
  82. in loose mode, rather than uppercasing them.
  83. * `xmlns` - Boolean. If true, then namespaces are supported.
  84. * `position` - Boolean. If false, then don't track line/col/position.
  85. * `strictEntities` - Boolean. If true, only parse [predefined XML
  86. entities](http://www.w3.org/TR/REC-xml/#sec-predefined-ent)
  87. (`&amp;`, `&apos;`, `&gt;`, `&lt;`, and `&quot;`)
  88. ## Methods
  89. `write` - Write bytes onto the stream. You don't have to do this all at
  90. once. You can keep writing as much as you want.
  91. `close` - Close the stream. Once closed, no more data may be written until
  92. it is done processing the buffer, which is signaled by the `end` event.
  93. `resume` - To gracefully handle errors, assign a listener to the `error`
  94. event. Then, when the error is taken care of, you can call `resume` to
  95. continue parsing. Otherwise, the parser will not continue while in an error
  96. state.
  97. ## Members
  98. At all times, the parser object will have the following members:
  99. `line`, `column`, `position` - Indications of the position in the XML
  100. document where the parser currently is looking.
  101. `startTagPosition` - Indicates the position where the current tag starts.
  102. `closed` - Boolean indicating whether or not the parser can be written to.
  103. If it's `true`, then wait for the `ready` event to write again.
  104. `strict` - Boolean indicating whether or not the parser is a jerk.
  105. `opt` - Any options passed into the constructor.
  106. `tag` - The current tag being dealt with.
  107. And a bunch of other stuff that you probably shouldn't touch.
  108. ## Events
  109. All events emit with a single argument. To listen to an event, assign a
  110. function to `on<eventname>`. Functions get executed in the this-context of
  111. the parser object. The list of supported events are also in the exported
  112. `EVENTS` array.
  113. When using the stream interface, assign handlers using the EventEmitter
  114. `on` function in the normal fashion.
  115. `error` - Indication that something bad happened. The error will be hanging
  116. out on `parser.error`, and must be deleted before parsing can continue. By
  117. listening to this event, you can keep an eye on that kind of stuff. Note:
  118. this happens *much* more in strict mode. Argument: instance of `Error`.
  119. `text` - Text node. Argument: string of text.
  120. `doctype` - The `<!DOCTYPE` declaration. Argument: doctype string.
  121. `processinginstruction` - Stuff like `<?xml foo="blerg" ?>`. Argument:
  122. object with `name` and `body` members. Attributes are not parsed, as
  123. processing instructions have implementation dependent semantics.
  124. `sgmldeclaration` - Random SGML declarations. Stuff like `<!ENTITY p>`
  125. would trigger this kind of event. This is a weird thing to support, so it
  126. might go away at some point. SAX isn't intended to be used to parse SGML,
  127. after all.
  128. `opentagstart` - Emitted immediately when the tag name is available,
  129. but before any attributes are encountered. Argument: object with a
  130. `name` field and an empty `attributes` set. Note that this is the
  131. same object that will later be emitted in the `opentag` event.
  132. `opentag` - An opening tag. Argument: object with `name` and `attributes`.
  133. In non-strict mode, tag names are uppercased, unless the `lowercase`
  134. option is set. If the `xmlns` option is set, then it will contain
  135. namespace binding information on the `ns` member, and will have a
  136. `local`, `prefix`, and `uri` member.
  137. `closetag` - A closing tag. In loose mode, tags are auto-closed if their
  138. parent closes. In strict mode, well-formedness is enforced. Note that
  139. self-closing tags will have `closeTag` emitted immediately after `openTag`.
  140. Argument: tag name.
  141. `attribute` - An attribute node. Argument: object with `name` and `value`.
  142. In non-strict mode, attribute names are uppercased, unless the `lowercase`
  143. option is set. If the `xmlns` option is set, it will also contains namespace
  144. information.
  145. `comment` - A comment node. Argument: the string of the comment.
  146. `opencdata` - The opening tag of a `<![CDATA[` block.
  147. `cdata` - The text of a `<![CDATA[` block. Since `<![CDATA[` blocks can get
  148. quite large, this event may fire multiple times for a single block, if it
  149. is broken up into multiple `write()`s. Argument: the string of random
  150. character data.
  151. `closecdata` - The closing tag (`]]>`) of a `<![CDATA[` block.
  152. `opennamespace` - If the `xmlns` option is set, then this event will
  153. signal the start of a new namespace binding.
  154. `closenamespace` - If the `xmlns` option is set, then this event will
  155. signal the end of a namespace binding.
  156. `end` - Indication that the closed stream has ended.
  157. `ready` - Indication that the stream has reset, and is ready to be written
  158. to.
  159. `noscript` - In non-strict mode, `<script>` tags trigger a `"script"`
  160. event, and their contents are not checked for special xml characters.
  161. If you pass `noscript: true`, then this behavior is suppressed.
  162. ## Reporting Problems
  163. It's best to write a failing test if you find an issue. I will always
  164. accept pull requests with failing tests if they demonstrate intended
  165. behavior, but it is very hard to figure out what issue you're describing
  166. without a test. Writing a test is also the best way for you yourself
  167. to figure out if you really understand the issue you think you have with
  168. sax-js.