Simple email application for Android. Original source code: https://framagit.org/dystopia-project/simple-email
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.

206 lines
6.2 KiB

  1. # redeyed [![build status](https://secure.travis-ci.org/thlorenz/redeyed.svg?branch=master)](http://travis-ci.org/thlorenz/redeyed)
  2. <a href="https://www.patreon.com/bePatron?u=8663953"><img alt="become a patron" src="https://c5.patreon.com/external/logo/become_a_patron_button.png" height="35px"></a>
  3. *Add color to your JavaScript!*
  4. ![frog](http://allaboutfrogs.org/gallery/photos/redeyes/red1.gif)
  5. [Red Eyed Tree Frog](http://allaboutfrogs.org/info/species/redeye.html) *(Agalychnis callidryas)*
  6. ## What?
  7. Takes JavaScript code, along with a config and returns the original code with tokens wrapped and/or replaced as configured.
  8. ## Where?
  9. - server side using nodejs
  10. - in the [browser](#browser-support)
  11. ## What for?
  12. One usecase is adding metadata to your code that can then be used to apply syntax highlighting.
  13. ## How?
  14. - copy the [config.js](https://github.com/thlorenz/redeyed/blob/master/config.js) and edit it in order to specify how
  15. certain tokens are to be surrounded/replaced
  16. - replace the `undefined` of each token you want to configure with one of the following
  17. ### {String} config
  18. `'before:after'`
  19. wraps the token inside before/after
  20. ### {Object} config
  21. `{ _before: 'before', _after: 'after' }`
  22. wraps token inside before/after
  23. #### Missing before and after resolution for {String} and {Object} config
  24. For the `{String}` and `{Object}` configurations, 'before' or 'after' may be omitted:
  25. - `{String}`:
  26. - `'before:'` (omitting 'after')
  27. - `':after'` (omitting 'before')
  28. - `{Object}`:
  29. - `{ _before: 'before' }` (omitting '_after')
  30. - `{ _after: 'after' }` (omitting '_before')
  31. In these cases the missing half is resolved as follows:
  32. - from the `parent._default` (i.e., `Keyword._default`) if found
  33. - otherwise from the `config._default` if found
  34. - otherwise `''` (empty string)
  35. ### {Function} config
  36. `function (tokenString, info) { return {String}|{Object}; }`
  37. #### Inputs
  38. - tokenString: the content of the token that is currently being processed
  39. - info: an object with the following structure
  40. ```js
  41. {
  42. // {Int}
  43. // the index of the token being processed inside tokens
  44. tokenIndex
  45. // {Array}
  46. // all tokens that are being processed including comments
  47. // (i.e. the result of merging esprima tokens and comments)
  48. , tokens
  49. // {Object}
  50. // the abstract syntax tree of the parsed code
  51. , ast
  52. // {String}
  53. // the code that was parsed (same string as the one passed to redeyed(code ..)
  54. , code
  55. }
  56. ```
  57. In most cases the `tokenString` is all you need. The extra info object is passed in case you need to gather more
  58. information about the `token`'s surroundings in order to decide how to transform it.
  59. See: [replace-log-example](https://github.com/thlorenz/redeyed/blob/master/examples/replace-log.js)
  60. #### Output
  61. You can return a {String} or an {Object} from a {Function} config.
  62. - when returning a {String}, the token value will be replaced with it
  63. - when returning an {Object}, it should be of the following form:
  64. ```js
  65. {
  66. // {String}
  67. // the string that should be substituted for the value of the current and all skipped tokens
  68. replacement
  69. // {Object} (Token)
  70. // the token after which processing should continue
  71. // all tokens in between the current one and this one inclusive will be ignored
  72. , skipPastToken
  73. }
  74. ```
  75. ### Transforming JavaScript code
  76. ***redeyed(code, config[, opts])***
  77. Invoke redeyed with your **config**uration, a **code** snippet and maybe **opts** as in the below example:
  78. ```javascript
  79. var redeyed = require('redeyed')
  80. , config = require('./path/to/config')
  81. , code = 'var a = 3;'
  82. , result;
  83. // redeyed will throw an error (caused by the esprima parser) if the code has invalid javascript
  84. try {
  85. result = redeyed(code, config);
  86. console.log(result.code);
  87. } catch(err) {
  88. console.error(err);
  89. }
  90. ```
  91. ***opts***:
  92. ```js
  93. { // {Boolean}
  94. // if true `result.ast` property contains the abstract syntax tree of the code
  95. // if false (default) `result.ast` is not assigned and therefore `undefined`
  96. buildAst: true|false
  97. // {Boolean}
  98. // if `true`, jsx syntax is supported, default `false`
  99. // due to how esprima works, the AST is built when this option is `true`, even if
  100. // `buildAST` is `false`
  101. , jsx: true|false
  102. // {Boolean}
  103. // if true `result.code` is not assigned and therefore `undefined`
  104. // if false (default) `result.code` property contains the result of `split.join`
  105. nojoin: true|false
  106. // {Object}
  107. // overrides default parser `esprima-fb` and needs to be compatible with it
  108. parser: require('esprima')
  109. }
  110. ```
  111. ***return value***:
  112. ```js
  113. { ast
  114. , tokens
  115. , comments
  116. , splits
  117. , code
  118. }
  119. ```
  120. - ast `{Array}`: [abstract syntax tree](http://en.wikipedia.org/wiki/Abstract_syntax_tree) as returned by [esprima
  121. parse](http://en.wikipedia.org/wiki/Abstract_syntax_tree)
  122. - tokens `{Array}`: [tokens](http://en.wikipedia.org/wiki/Token_(parser)) provided by esprima (excluding
  123. comments)
  124. - comments `{Array}`: block and line comments as provided by esprima
  125. - splits `{Array}`: code pieces split up, some of which where transformed as configured
  126. - code `{String}`: transformed code, same as `splits.join('')` unless this step has been skipped (see opts)
  127. ## Browser Support
  128. ### AMD
  129. Ensure to include [esprima](https://github.com/ariya/esprima) as one of your dependencies
  130. ```js
  131. define(['redeyed'], function (redeyed) {
  132. [ .. ]
  133. });
  134. ```
  135. ### Attached to global window object
  136. The `redeyed {Function}` will be exposed globally as `window.redeyed` - big surprise!
  137. ```html
  138. <script type="text/javascript" src="https://unpkg.com/esprima"></script>
  139. <script type="text/javascript" src="https://unpkg.com/redeyed"></script>
  140. ```
  141. ## redeyed in the wild
  142. - [cardinal](https://github.com/thlorenz/cardinal): Syntax highlights JavaScript code with ANSI colors to be printed to
  143. the terminal
  144. - [peacock](http://thlorenz.github.com/peacock/): JavaScript syntax highlighter that generates html that is compatible
  145. with pygments styles.
  146. ## Examples
  147. - `npm explore redeyed; npm demo` will let you try the [browser example](https://github.com/thlorenz/redeyed/tree/master/examples/browser)
  148. - `npm explore redeyed; npm demo-log` will let you try the [replace log example](https://github.com/thlorenz/redeyed/blob/master/examples/replace-log.js)