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 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  1. # Gonzales PE @dev
  2. [![NPM version][npm-img]][npm]
  3. [![Build Status][travis-img]][travis]
  4. [![AppVeyor Build Status][appveyor-img]][appveyor]
  5. [npm-img]: https://img.shields.io/npm/v/gonzales-pe.svg
  6. [npm]: https://www.npmjs.com/package/gonzales-pe
  7. [travis-img]: https://travis-ci.org/tonyganch/gonzales-pe.svg
  8. [travis]: https://travis-ci.org/tonyganch/gonzales-pe
  9. [appveyor-img]: https://ci.appveyor.com/api/projects/status/m29jphtrqt398v2o/branch/dev?svg=true
  10. [appveyor]: https://ci.appveyor.com/project/tonyganch/gonzales-pe/branch/dev
  11. Gonzales PE is a CSS parser which plays nicely with preprocessors.
  12. Currently those are supported: SCSS, Sass, LESS.
  13. Try out Gonzales PE online: [Gonzales PE Playground](https://tonyganch.io/gonzales-pe/).
  14. ## Install
  15. (1) To install command-line tool globally:
  16. ```bash
  17. npm install -g git://github.com/tonyganch/gonzales-pe.git#dev
  18. ```
  19. (2) To install parser as a project dependency:
  20. ```bash
  21. npm install --save git://github.com/tonyganch/gonzales-pe.git#dev
  22. ```
  23. (3) If for some reason you want to build files yourself:
  24. ```bash
  25. # Clone the repo.
  26. git clone git@github.com:tonyganch/gonzales-pe.git
  27. # Go to dev branch.
  28. git checkout dev
  29. # Install project dependencies.
  30. npm install
  31. # Install git hooks and build files.
  32. npm run init
  33. ```
  34. ## API
  35. Basically there are a few things you can do:
  36. 1. parse css string and get a parse tree in return;
  37. 2. modify tree nodes;
  38. 3. remove tree nodes;
  39. 4. add new nodes to the tree;
  40. 5. convert modified tree back to a string.
  41. The different type of tree nodes can be found in [docs/node-types.md](https://github.com/tonyganch/gonzales-pe/blob/dev/docs/node-types.md).
  42. In examples below I assume that `gonzales` is a parser module and `parseTree`
  43. is some parsed css:
  44. ```js
  45. let gonzales = require('gonzales-pe');
  46. let parseTree = gonzales.parse(css);
  47. ```
  48. ### gonzales.createNode(options)
  49. ##### Description
  50. Creates a new node. Useful when you need to add something to a tree.
  51. ##### Parameters
  52. <table>
  53. <tr>
  54. <td><i>{Object}</i></td>
  55. <td>options</td>
  56. <td>Options to pass to a `Node` constructor.</td>
  57. </tr>
  58. <tr>
  59. </table>
  60. ##### Returns
  61. <table>
  62. <tr>
  63. <td><i>{Object}</i></td>
  64. <td>A new node.</td>
  65. </tr>
  66. </table>
  67. ##### Examples
  68. ```js
  69. let css = 'a {color: tomato}';
  70. let parseTree = gonzales.parse(css);
  71. let node = gonzales.createNode({ type: 'animal', content: 'panda' });
  72. parseTree.content.push(node);
  73. ```
  74. ### gonzales.parse(css[, options])
  75. ##### Description
  76. Parses a css string.
  77. ##### Parameters
  78. <table>
  79. <tr>
  80. <td><i>{string}</i></td>
  81. <td>css</td>
  82. <td>A string to parse.</td>
  83. </tr>
  84. <tr>
  85. <td><i>{Object=}</i></td>
  86. <td>options</td>
  87. <td>Optional. Additional options:
  88. <ul>
  89. <li>
  90. <code>{string} syntax</code> — any of the following: <code>css</code>,
  91. <code>less</code>, <code>sass</code>, <code>scss</code>.
  92. Default one is <code>css</code>.
  93. </li>
  94. <li>
  95. <code>{string} context</code> — root node's type. For a list of available
  96. values see <a href="docs/node-types.md">"Node types"</a>. Default
  97. one is <code>stylesheet</code>.
  98. </li>
  99. <li>
  100. <code>{number} tabSize</code> — size of a tab character in spaces.
  101. Default one is 1.
  102. </li>
  103. </ul>
  104. </td>
  105. </tr>
  106. </table>
  107. ##### Returns
  108. <table>
  109. <tr>
  110. <td><i>{Object}</i></td>
  111. <td>Parse tree.</td>
  112. </tr>
  113. </table>
  114. ##### Examples
  115. ```js
  116. let css = 'a {color: tomato}';
  117. let parseTree = gonzales.parse(css);
  118. ```
  119. ```js
  120. let less = 'a {$color: tomato}';
  121. let parseTree = gonzales.parse(less, {syntax: 'less'});
  122. ```
  123. ```js
  124. let less = '$color: tomato';
  125. let parseTree = gonzales.parse(less, {syntax: 'less', rule: 'declaration'});
  126. ```
  127. ### parseTree.contains(type)
  128. ##### Description
  129. Checks whether there is a child node of given type.
  130. ##### Parameters
  131. <table>
  132. <tr>
  133. <td><i>{string}</i></td>
  134. <td>type</td>
  135. <td>
  136. Node type we're looking for. For a list of available values see
  137. <a href="docs/node-types.md">"Node types"</a>.
  138. </td>
  139. </tr>
  140. </table>
  141. ##### Returns
  142. <table>
  143. <tr>
  144. <td><i>{boolean}</i></td>
  145. <td>
  146. <code>true</code> if a tree contains a child node of a given type,
  147. <code>false</code> otherwise.
  148. </td>
  149. </tr>
  150. </table>
  151. ##### Examples
  152. ```js
  153. if (parseTree.contains('space')) {
  154. doSomething();
  155. }
  156. ```
  157. ### parseTree.content
  158. ##### Returns
  159. <table>
  160. <tr>
  161. <td><i>{string|Array}</i></td>
  162. <td>Node's content (child nodes or a string).</td>
  163. </tr>
  164. </table>
  165. ### parseTree.eachFor([type, ]callback)
  166. ##### Description
  167. Calls a function for every child node in tree. If `type` parameter is passed,
  168. calls a function only for child nodes of a given type. The main difference from
  169. `parseTree.forEach()` is that this method loops through node list from the end
  170. to beginning.
  171. ##### Parameters
  172. <table>
  173. <tr>
  174. <td><i>{string=}</i></td>
  175. <td>type</td>
  176. <td>
  177. Optional. A node type by which to filter child nodes before applying
  178. a callback function. For a list of available values see
  179. <a href="docs/node-types.md">"Node types"</a>.
  180. </td>
  181. </tr>
  182. <tr>
  183. <td><i>{Function}</i></td>
  184. <td>callback</td>
  185. <td>
  186. Function to call for every child node. Accepts following parameters:
  187. <ul>
  188. <li><code>{Object}</code> — a child node;</li>
  189. <li><code>{number}</code> — index of the child node in node list;</li>
  190. <li>
  191. <code>{Object}</code> — parent node (equals to <code>parseTree</code>).
  192. </li>
  193. </ul>
  194. </td>
  195. </tr>
  196. </table>
  197. ##### Examples
  198. ```js
  199. parseTree.eachFor(function(childNode) {
  200. doSomething(childNode);
  201. });
  202. ```
  203. ```js
  204. // Remove all child spaces.
  205. parseTree.eachFor('space', function(spaceNode, i) {
  206. parseTree.removeChild(i);
  207. });
  208. ```
  209. ### parseTree.end
  210. ##### Returns
  211. <table>
  212. <tr>
  213. <td><i>{Object}</i></td>
  214. <td>
  215. End position of the node. Contains following info:
  216. <ul>
  217. <li>
  218. <code>{number} line</code> — last symbol's line number
  219. (1-based index);
  220. </li>
  221. <li>
  222. <code>{number} column</code> — last symbol's column number
  223. (1-based index).
  224. </li>
  225. </ul>
  226. </td>
  227. </tr>
  228. </table>
  229. ### parseTree.first([type])
  230. ##### Description
  231. Gets the first child node. If `type` parameter is passed, gets the first child
  232. node of a given type. If no node has been found, returns `null`.
  233. ##### Parameters
  234. <table>
  235. <tr>
  236. <td><i>{string=}</i></td>
  237. <td>type</td>
  238. <td>
  239. Optional. Node type to look for. For a list of available values see
  240. <a href="docs/node-types.md">"Node types"</a>.
  241. </td>
  242. </tr>
  243. </table>
  244. ##### Returns
  245. <table>
  246. <tr>
  247. <td><i>{?Object}</i></td>
  248. <td>A node.</td>
  249. </tr>
  250. </table>
  251. ##### Examples
  252. ```js
  253. let node = parseTree.first();
  254. node.content = 'panda';
  255. ```
  256. ```js
  257. let node = parseTree.first('multilineComment');
  258. node.content = 'panda';
  259. ```
  260. ### parseTree.forEach([type, ]callback)
  261. ##### Description
  262. Calls a function for every child node in tree. If `type` parameter is passed,
  263. calls a function only for child nodes of a given type. The main difference from
  264. `parseTree.eachFor()` is that this method loops through node list from the
  265. beginnig to end.
  266. ##### Parameters
  267. <table>
  268. <tr>
  269. <td><i>{string=}</i></td>
  270. <td>type</td>
  271. <td>
  272. Optional. A node type by which to filter child nodes before applying
  273. a callback function. For a list of available values see
  274. <a href="docs/node-types.md">"Node types"</a>.
  275. </td>
  276. </tr>
  277. <tr>
  278. <td><i>{Function}</i></td>
  279. <td>callback</td>
  280. <td>
  281. Function to call for every child node. Accepts following parameters:
  282. <ul>
  283. <li><code>{Object}</code> — a child node;</li>
  284. <li><code>{number}</code> — index of the child node in node list;</li>
  285. <li>
  286. <code>{Object}</code> — parent node (equals to <code>parseTree</code>).
  287. </li>
  288. </ul>
  289. </td>
  290. </tr>
  291. </table>
  292. ##### Examples
  293. ```js
  294. parseTree.forEach(function(childNode) {
  295. doSomething();
  296. });
  297. ```
  298. ```js
  299. parseTree.forEach('space', function(spaceNode) {
  300. doSomething();
  301. });
  302. ```
  303. ### parseTree.get(index)
  304. ##### Description
  305. Gets *nth* child of the `parseTree`. If no node has been found, returns `null`.
  306. ##### Parameters
  307. <table>
  308. <tr>
  309. <td><i>{number}</i></td>
  310. <td>index</td>
  311. <td>Index number of node which we're looking for.</td>
  312. </tr>
  313. </table>
  314. ##### Returns
  315. <table>
  316. <tr>
  317. <td><i>{?Object}</i></td>
  318. <td>A node.</td>
  319. </tr>
  320. </table>
  321. ##### Examples
  322. ```js
  323. var node = parseTree.get(2);
  324. doSomething(node);
  325. ```
  326. ### parseTree.insert(index, node)
  327. ##### Description
  328. Inserts a node to a given position in `parseTree`.
  329. ##### Parameters
  330. <table>
  331. <tr>
  332. <td><i>{number}</i></td>
  333. <td>index</td>
  334. <td>Index of position where to insert the node.</td>
  335. </tr>
  336. <tr>
  337. <td><i>{Object}</i></td>
  338. <td>node</td>
  339. <td>A node to insert.</td>
  340. </tr>
  341. </table>
  342. ##### Examples
  343. ```js
  344. let node = gonzales.createNode({type: 'animal', content: 'panda'});
  345. parseTree.insert(2, node);
  346. ```
  347. ### parseTree.is(type)
  348. ##### Description
  349. Checks whether the node is of given type.
  350. ##### Parameters
  351. <table>
  352. <tr>
  353. <td><i>{string}</i></td>
  354. <td>type</td>
  355. <td>
  356. A node type against which to check type of <code>parseTree</code>.
  357. For a list of available values see
  358. <a href="docs/node-types.md">"Node types"</a>.
  359. </td>
  360. </tr>
  361. </table>
  362. ##### Returns
  363. <table>
  364. <tr>
  365. <td><i>{boolean}</i></td>
  366. <td>
  367. <code>true</code> if types are equal, <code>false</code> otherwise.
  368. </td>
  369. </tr>
  370. </table>
  371. ##### Examples
  372. ```js
  373. if (node.is('space')) {
  374. node.content = '';
  375. }
  376. ```
  377. ### parseTree.last(type)
  378. Gets the last child node. If `type` parameter is passed, gets the last child
  379. node of a given type. If no node has been found, returns `null`.
  380. ##### Parameters
  381. <table>
  382. <tr>
  383. <td><i>{string=}</i></td>
  384. <td>type</td>
  385. <td>
  386. Optional. Node type to look for. For a list of available values see
  387. <a href="docs/node-types.md">"Node types"</a>.
  388. </td>
  389. </tr>
  390. </table>
  391. ##### Returns
  392. <table>
  393. <tr>
  394. <td><i>{?Object}</i></td>
  395. <td>A node.</td>
  396. </tr>
  397. </table>
  398. ##### Examples
  399. ```js
  400. let node = parseTree.last();
  401. node.content = 'panda';
  402. ```
  403. ```js
  404. let node = parseTree.last('multilineComment');
  405. node.content = 'panda';
  406. ```
  407. ### parseTree.length
  408. ##### Returns
  409. <table>
  410. <tr>
  411. <td><i>{number}</i></td>
  412. <td>Number of child nodes.</td>
  413. </tr>
  414. </table>
  415. ### parseTree.removeChild(index)
  416. ##### Description
  417. Removes a child node at a given position.
  418. ##### Parameters
  419. <table>
  420. <tr>
  421. <td><i>{number}</i></td>
  422. <td>index</td>
  423. <td>Index of a child node we need to remove.</td>
  424. </tr>
  425. </table>
  426. ##### Returns
  427. <table>
  428. <tr>
  429. <td><i>{Object}</i></td>
  430. <td>Removed node.</td>
  431. </tr>
  432. </table>
  433. ##### Examples
  434. ```js
  435. // Swap nodes.
  436. var node = parseTree.removeChild(1);
  437. parseTree.insert(0, node);
  438. ```
  439. ### parseTree.start
  440. ##### Returns
  441. <table>
  442. <tr>
  443. <td><i>{Object}</i></td>
  444. <td>
  445. Start position of the node. Contains following info:
  446. <ul>
  447. <li>
  448. <code>{number} line</code> — first symbol's line number
  449. (1-based index);
  450. </li>
  451. <li>
  452. <code>{number} column</code> — first symbol's column number
  453. (1-based index).
  454. </li>
  455. </ul>
  456. </td>
  457. </tr>
  458. </table>
  459. ### parseTree.syntax
  460. ##### Returns
  461. <table>
  462. <tr>
  463. <td><i>{string}</i></td>
  464. <td>Syntax of original parsed string.</td>
  465. </tr>
  466. </table>
  467. ### parseTree.toJson()
  468. ##### Description
  469. Converts parse tree to JSON. Useful for printing.
  470. ##### Returns
  471. <table>
  472. <tr>
  473. <td><i>{Object}</i></td>
  474. <td>Parse tree in JSON</td>
  475. </tr>
  476. </table>
  477. ### parseTree.toString()
  478. ##### Description
  479. Converts parse tree back to string according to original syntax.
  480. ##### Returns
  481. <table>
  482. <tr>
  483. <td><i>{string}</i></td>
  484. <td>A compiled string.</td>
  485. </tr>
  486. </table>
  487. ##### Examples
  488. ```js
  489. let css = parseTree.toString();
  490. ```
  491. ### parseTree.traverse(callback)
  492. ##### Description
  493. Calls the function for every node in a tree including `parseTree` itself.
  494. ##### Parameters
  495. <table>
  496. <tr>
  497. <td><i>{Function}</i></td>
  498. <td>callback</td>
  499. <td>
  500. Function to apply to every node. Accepts following parameters:
  501. <ul>
  502. <li><code>{Object}</code> — a node to which we apply callback;</li>
  503. <li><code>{number}</code> — node's index number inside its parent;</li>
  504. <li><code>{Object}</code> — a node's parent;</li>
  505. <li>
  506. <code>{number}</code> — node's nesting level relative to its parent.
  507. </li>
  508. </ul>
  509. </td>
  510. </tr>
  511. </table>
  512. ##### Examples
  513. ```js
  514. parseTree.traverse(function(node, index, parent) {
  515. if (node.is('multilineComment')) {
  516. parent.removeChild(index);
  517. } else if (node.is('space')) {
  518. node.content = ' ';
  519. }
  520. });
  521. ```
  522. ### parseTree.traverseByType(type, callback)
  523. ##### Description
  524. This method should be called for a root node, because calling it for a child
  525. will be more time consuming.
  526. Calls the function for every node of a given type. This means not just child
  527. nodes, but grandchilds and so on.
  528. ##### Parameters
  529. <table>
  530. <tr>
  531. <td><i>{string}</i></td>
  532. <td>type</td>
  533. <td>
  534. Node type. For a list of available values please see
  535. <a href="docs/node-types.md">"Node types"</a>.
  536. </td>
  537. </tr>
  538. <tr>
  539. <td><i>{Function}</i></td>
  540. <td>callback</td>
  541. <td>
  542. Function to apply to every node of a given type.
  543. Accepts following parameters:
  544. <ul>
  545. <li><code>{Object}</code> — a node to which we apply callback;</li>
  546. <li><code>{number}</code> — node's index number inside its parent;</li>
  547. <li><code>{Object}</code> — a node's parent.</li>
  548. </ul>
  549. </td>
  550. </tr>
  551. </table>
  552. ##### Examples
  553. ```js
  554. // Remove all comments.
  555. parseTree.traverseByType('multilineComment', function(node, index, parent) {
  556. parent.removeChild(index);
  557. });
  558. ```
  559. ### parseTree.traverseByTypes(types, callback)
  560. ##### Description
  561. This method should be called for a root node, because calling it for a child
  562. will be more time consuming.
  563. Calls the function for every node of given types. This means not just child
  564. nodes, but grandchilds and so on.
  565. ##### Parameters
  566. <table>
  567. <tr>
  568. <td><i>{Array.string}</i></td>
  569. <td>types</td>
  570. <td>
  571. List of node types. For a list of available values please see
  572. <a href="docs/node-types.md">"Node types"</a>.
  573. </td>
  574. </tr>
  575. <tr>
  576. <td><i>{Function}</i></td>
  577. <td>callback</td>
  578. <td>
  579. Function to apply to every node of given types.
  580. Accepts following parameters:
  581. <ul>
  582. <li><code>{Object}</code> — a node to which we apply callback;</li>
  583. <li><code>{number}</code> — node's index number inside its parent;</li>
  584. <li><code>{Object}</code> — a node's parent.</li>
  585. </ul>
  586. </td>
  587. </tr>
  588. </table>
  589. ##### Examples
  590. ```js
  591. // Remove all comments and spaces.
  592. let types = ['multilineComment', 'space'];
  593. parseTree.traverseByTypes(types, function(node, index, parent) {
  594. parent.removeChild(index);
  595. });
  596. ```
  597. ### parseTree.type
  598. ##### Returns
  599. <table>
  600. <tr>
  601. <td><i>{string}</i></td>
  602. <td>
  603. Node type. For a list of available values see
  604. <a href="docs/node-types.md">"Node types"</a>.
  605. </td>
  606. </tr>
  607. </table>
  608. ## Test
  609. To run tests:
  610. npm test
  611. This command will build library files from sources and run tests on all files
  612. in syntax directories.
  613. Every test has 3 files: source stylesheet, expected parse tree and expected
  614. string compiled back from parse tree to css.
  615. If some tests fail, you can find information in test logs:
  616. - `log/test.log` contains all information from stdout;
  617. - `log/expected.txt` contains only expected text;
  618. - `log/result.txt` contains only result text.
  619. The last two are made for your convenience: you can use any diff app to see
  620. the defference between them.
  621. If you want to test one specific string or get a general idea of how Gonzales
  622. works, you can use `test/ast.js` file.
  623. Simply change the first two strings (`css` and `syntax` vars) and run:
  624. node test/single-test.js
  625. ## Report
  626. If you find a bug or want to add a feature, welcome to [Issues](https://github.com/tonyganch/gonzales-pe/issues).
  627. If you are shy but have a question, feel free to [drop me a
  628. line](mailto:tonyganch+gonzales@gmail.com).