Skip to content

Commit a1dfd54

Browse files
committedApr 16, 2015
Merge pull request #509 from openannotation/clean-up-markdown-tags
Clean up markdown and tags modules
2 parents 6d7fd50 + 9c1331d commit a1dfd54

File tree

12 files changed

+425
-345
lines changed

12 files changed

+425
-345
lines changed
 

‎doc/api/ui.rst

+6
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,9 @@ annotator.ui package
55

66
.. include:: ui/main.rst
77
:start-line: 5
8+
9+
.. include:: ui/markdown.rst
10+
:start-line: 5
11+
12+
.. include:: ui/tags.rst
13+
:start-line: 5

‎doc/api/ui/main.rst

+18
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,23 @@ annotator.ui package
1919

2020
A DOM element to which to bind event listeners. Defaults to
2121
``document.body``, allowing annotation of the whole document.
22+
23+
.. attribute:: options.editorExtensions
24+
25+
An array of editor extensions. See the
26+
:class:`~annotator.ui.editor.Editor` documentation for details of editor
27+
extensions.
28+
29+
.. attribute:: options.viewerExtensions
30+
31+
An array of viewer extensions. See the
32+
:class:`~annotator.ui.viewer.Viewer` documentation for details of viewer
33+
extensions.
34+
35+
.. attribute:: options.viewerRenderer
36+
37+
An annotation renderer for the viewer. See the
38+
:class:`~annotator.ui.viewer.Viewer` documentation for details of
39+
renderers.
2240

2341

‎doc/api/ui/markdown.rst

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
.. default-domain: js
2+
3+
annotator.ui.markdown package
4+
=============================
5+
6+
.. function:: annotator.ui.markdown.renderer(annotation)
7+
8+
A renderer for the :class:`~annotator.ui.viewer.Viewer` which interprets
9+
annotation text as `Markdown`_ and uses the `Showdown`_ library if present in
10+
the page to render annotations with Markdown text as HTML.
11+
12+
.. _Markdown: https://daringfireball.net/projects/markdown/
13+
.. _Showdown: https://github.com/showdownjs/showdown
14+
15+
**Usage**::
16+
17+
app.include(annotator.ui.main, {
18+
viewerRenderer: annotator.ui.markdown.renderer
19+
});
20+
21+

‎doc/api/ui/tags.rst

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
.. default-domain: js
2+
3+
annotator.ui.tags package
4+
=========================
5+
6+
.. function:: annotator.ui.tags.viewerExtension(viewer)
7+
8+
An extension for the :class:`~annotator.ui.viewer.Viewer` which displays any
9+
tags stored as an array of strings in the annotation's ``tags`` property.
10+
11+
**Usage**::
12+
13+
app.include(annotator.ui.main, {
14+
viewerExtensions: [annotator.ui.tags.viewerExtension]
15+
})
16+
17+
18+
.. function:: annotator.ui.tags.editorExtension(editor)
19+
20+
An extension for the :class:`~annotator.ui.editor.Editor` which allows
21+
editing a set of space-delimited tags, retrieved from and saved to the
22+
annotation's ``tags`` property.
23+
24+
**Usage**::
25+
26+
app.include(annotator.ui.main, {
27+
viewerExtensions: [annotator.ui.tags.viewerExtension]
28+
})
29+
30+

‎src/ui/main.js

+36-33
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ var util = require('../util');
66
var adder = require('./adder');
77
var editor = require('./editor');
88
var highlighter = require('./highlighter');
9-
var markdown = require('./markdown');
10-
var tags = require('./tags');
119
var textselector = require('./textselector');
1210
var viewer = require('./viewer');
1311

@@ -194,20 +192,37 @@ function addPermissionsCheckboxes(editor, app) {
194192
*
195193
* A DOM element to which to bind event listeners. Defaults to
196194
* ``document.body``, allowing annotation of the whole document.
195+
*
196+
* .. attribute:: options.editorExtensions
197+
*
198+
* An array of editor extensions. See the
199+
* :class:`~annotator.ui.editor.Editor` documentation for details of editor
200+
* extensions.
201+
*
202+
* .. attribute:: options.viewerExtensions
203+
*
204+
* An array of viewer extensions. See the
205+
* :class:`~annotator.ui.viewer.Viewer` documentation for details of viewer
206+
* extensions.
207+
*
208+
* .. attribute:: options.viewerRenderer
209+
*
210+
* An annotation renderer for the viewer. See the
211+
* :class:`~annotator.ui.viewer.Viewer` documentation for details of
212+
* renderers.
213+
*
197214
*/
198215
function main(options) {
199216
if (typeof options === 'undefined' || options === null) {
200217
options = {};
201218
}
202219

203-
var element = options.element || util.getGlobal().document.body;
204-
// FIXME: restore readOnly mode
205-
//
206-
// options: # Configuration options
207-
// # Start Annotator in read-only mode. No controls will be shown.
208-
// readOnly: false
220+
options.element = options.element || util.getGlobal().document.body;
221+
options.editorExtensions = options.editorExtensions || [];
222+
options.viewerExtensions = options.viewerExtensions || [];
223+
209224
// Local helpers
210-
var makeAnnotation = annotationFactory(element, '.annotator-hl');
225+
var makeAnnotation = annotationFactory(options.element, '.annotator-hl');
211226

212227
// Object to hold local state
213228
var s = {
@@ -222,15 +237,16 @@ function main(options) {
222237
});
223238
s.adder.attach();
224239

225-
s.tags = tags.tags({});
226-
s.editor = new editor.Editor({extensions: [s.tags.createEditorField]});
240+
s.editor = new editor.Editor({
241+
extensions: options.editorExtensions
242+
});
227243
s.editor.attach();
228244

229245
addPermissionsCheckboxes(s.editor, app);
230246

231-
s.highlighter = new highlighter.Highlighter(element);
247+
s.highlighter = new highlighter.Highlighter(options.element);
232248

233-
s.textselector = new textselector.TextSelector(element, {
249+
s.textselector = new textselector.TextSelector(options.element, {
234250
onSelection: function (ranges, event) {
235251
if (ranges.length > 0) {
236252
var annotation = makeAnnotation(ranges);
@@ -242,7 +258,7 @@ function main(options) {
242258
}
243259
});
244260

245-
var viewerOpts = {
261+
s.viewer = new viewer.Viewer({
246262
onEdit: function (ann) {
247263
// Copy the interaction point from the shown viewer:
248264
s.interactionPoint = util.$(s.viewer.element)
@@ -254,28 +270,15 @@ function main(options) {
254270
app.annotations['delete'](ann);
255271
},
256272
permitEdit: function (ann) {
257-
return app.authz.permits(
258-
'update',
259-
ann,
260-
app.ident.who()
261-
);
273+
return app.authz.permits('update', ann, app.ident.who());
262274
},
263275
permitDelete: function (ann) {
264-
return app.authz.permits(
265-
'delete',
266-
ann,
267-
app.ident.who()
268-
);
276+
return app.authz.permits('delete', ann, app.ident.who());
269277
},
270-
extensions: [s.tags.createViewerField],
271-
autoViewHighlights: element
272-
};
273-
274-
if (g.Showdown && typeof g.Showdown.converter === 'function') {
275-
viewerOpts.renderText = markdown.markdown().convert;
276-
}
277-
278-
s.viewer = new viewer.Viewer(viewerOpts);
278+
autoViewHighlights: options.element,
279+
extensions: options.viewerExtensions,
280+
renderer: options.viewerRenderer
281+
});
279282
s.viewer.attach();
280283

281284
injectDynamicStyle();

‎src/ui/markdown.js

+28-25
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,41 @@
1+
/*package annotator.ui.markdown */
12
"use strict";
23

34
var util = require('../util');
45

56
var g = util.getGlobal();
67
var _t = util.gettext;
78

8-
function markdown() {
9-
var converter = null;
9+
10+
/**
11+
* function:: renderer(annotation)
12+
*
13+
* A renderer for the :class:`~annotator.ui.viewer.Viewer` which interprets
14+
* annotation text as `Markdown`_ and uses the `Showdown`_ library if present in
15+
* the page to render annotations with Markdown text as HTML.
16+
*
17+
* .. _Markdown: https://daringfireball.net/projects/markdown/
18+
* .. _Showdown: https://github.com/showdownjs/showdown
19+
*
20+
* **Usage**::
21+
*
22+
* app.include(annotator.ui.main, {
23+
* viewerRenderer: annotator.ui.markdown.renderer
24+
* });
25+
*/
26+
exports.renderer = function renderer(annotation) {
27+
var convert = util.escapeHtml;
1028

1129
if (g.Showdown && typeof g.Showdown.converter === 'function') {
12-
converter = new g.Showdown.converter();
30+
convert = new g.Showdown.converter().makeHtml;
1331
} else {
14-
console.warn(_t("To use the Markdown plugin, you must" +
15-
" include Showdown into the page first."));
32+
console.warn(_t("To use the Markdown plugin, you must " +
33+
"include Showdown into the page first."));
1634
}
1735

18-
// Converts provided text into markdown.
19-
//
20-
// text - A String of Markdown to render as HTML.
21-
//
22-
// Examples
23-
//
24-
// plugin.convert('This is _very_ basic [Markdown](http://daringfireball.com)')
25-
// # => Returns "This is <em>very<em> basic <a href="http://...">Markdown</a>"
26-
//
27-
// Returns HTML string.
28-
function convert (text) {
29-
text = util.escapeHtml(text || '');
30-
return converter ? converter.makeHtml(text) : text;
36+
if (annotation.text) {
37+
return convert(annotation.text);
38+
} else {
39+
return "<i>" + _t('No comment') + "</i>";
3140
}
32-
33-
return {
34-
convert: convert
35-
};
36-
}
37-
38-
exports.markdown = markdown;
41+
};

0 commit comments

Comments
 (0)
Please sign in to comment.