OscarGodson/EpicEditor — pull request #114 by johnmdonahue (2012-05-21 02:45:36+00:00)
Title: Parser as option

A bunch of updates here so let me break it down. Also I realize I didn't get much done on tests specifically, but the good news is, it now passes the linter :) ... and I'll be cleaning that up more this week.

1. __Added linting for other JS:__ Now `jake lint` will check all of the JS files listed below. `jake build`, `jake docs`, and `jake test` only lint their associated files. I'm not sure if you would want a force option for each so that is currently only available for `build`.
* `src/editor.js`
* `Jakefile.js` (Note: `jshint` does not currently support files without ext so .js was added.)
* `.jshintrc` (just because it's funny to lint a lint config)
* `docs/js/main.js`
* `spec/spec.js`
2. __Pulled Marked out of EE wrapper:__ The intro.js and tmp file concat is no longer necessary. The default EE build will still use Marked but an optional parser can be provided by the user as described in 3. The only real drawback of this is that it creates an additional global but this seems like a reasonable thing to accept in trade for the flexibility it offers.
3. __Added settings.parser:__ This option accepts any function that returns a string. If the user has included `Showdown` or some textile parser on the page, the parsing function can be passed into this option otherwise EE will look for Marked by default and if that is not available, use a generic function that returns an error message followed by the original content wrapped in `

` tags (See example below.). Putting the parser on the settings object seems like a great option rather than extending the EE object as we had discussed. I am imagining an implementation where a user is allowed to select between textile or markdown to parse their content. As a part of the settings object, this parser could be toggled at any point for a given instance and still function (e.g. realize your content is textile?... just switch the parser).
4. __Added build option to use custom parser:__ Running `jake build parser=path/to/parser.js` will override the default Marked build and replace it with the custom script. You can play around with this as follows:

Clone Showdown:

```bash
$ git clone https://github.com/coreyti/showdown.git
```

Build EE using Showdown:

```bash
$ jake build parser=path/to/showdown/src/showdown.js
```

Modify `docs/js/main.js` to point to the Showdown parser:

```javascript
var opts =
{ container: 'example-1'
, file: { defaultContent: "#EpicEditor\nThis is some default content. Go ahead, _change me_. " }
, focusOnLoad: true
, parser: new Showdown.converter()
}
```

Whoops! That's not the rendering function.

![](http://f.cl.ly/items/3t1x0H0F1G2T2k3E2N2a/Screen%20Shot%202012-05-20%20at%205.19.07%20PM.png)

Now correct the parser:

```javascript
var opts =
{ container: 'example-1'
, file: { defaultContent: "#EpicEditor\nThis is some default content. Go ahead, _change me_. " }
, focusOnLoad: true
, parser: new Showdown.converter().makeHtml
}
```

Good to go.

## Current Jake tasks

```bash
jake lint # Lint all js files
jake lint:editor # Lint core EpicEditor: src/editor.js
jake lint:docs # Lint doc related js: docs/js/main.js
jake lint:spec # Lint test related js: spec/spec.js
jake lint:util # Lint utility and config js files
jake build # Build epiceditor.js and minify to epiceditor.min.js
jake build:force # Force build epiceditor.js and epiceditor.min.js skipping pre-reqs
jake docs # Build index.html from the README
jake test # Test code against specs
jake package # Build the package for distribution
jake clobber # Remove the package
jake ascii # Kick out some ascii
```