You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@allura.apache.org by br...@apache.org on 2015/09/11 21:34:16 UTC
[4/6] allura git commit: [#7955] WIP: basic fenced removal working
[#7955] WIP: basic fenced removal working
Project: http://git-wip-us.apache.org/repos/asf/allura/repo
Commit: http://git-wip-us.apache.org/repos/asf/allura/commit/22ed50fa
Tree: http://git-wip-us.apache.org/repos/asf/allura/tree/22ed50fa
Diff: http://git-wip-us.apache.org/repos/asf/allura/diff/22ed50fa
Branch: refs/heads/db/7955
Commit: 22ed50fa76b7e6d2dd7f287c7829dbcc44fb206b
Parents: fdf6c7c
Author: Dave Brondsema <db...@slashdotmedia.com>
Authored: Thu Sep 10 20:40:37 2015 +0000
Committer: Dave Brondsema <db...@slashdotmedia.com>
Committed: Thu Sep 10 20:40:37 2015 +0000
----------------------------------------------------------------------
.../lib/widgets/resources/js/sf_markitup.js | 73 ++++++++++----------
1 file changed, 35 insertions(+), 38 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/allura/blob/22ed50fa/Allura/allura/lib/widgets/resources/js/sf_markitup.js
----------------------------------------------------------------------
diff --git a/Allura/allura/lib/widgets/resources/js/sf_markitup.js b/Allura/allura/lib/widgets/resources/js/sf_markitup.js
index 61a21ed..8aeb551 100644
--- a/Allura/allura/lib/widgets/resources/js/sf_markitup.js
+++ b/Allura/allura/lib/widgets/resources/js/sf_markitup.js
@@ -100,6 +100,15 @@ $(window).load(function() {
function toggleCodeBlockTildes(editor) {
// NOTE: this requires highlightFormatting:true in the mode config
+
+ function fencing_line(line) {
+ /* return true, if this is a ``` or ~~~ line */
+ if (typeof line !== 'object') {
+ throw 'fencing_line() takes a "line" object (not a line number, or line text). Got: ' + typeof line + ': ' + line;
+ }
+ return line.styles && line.styles.length > 2 && line.styles[2].indexOf('formatting-code-block') !== -1;
+ }
+
function code_type(cm, line_num, line, firstTok, lastTok) {
/*
* Return 'single', 'indented', 'fenced' or false
@@ -117,8 +126,7 @@ $(window).load(function() {
} else if (types.indexOf('comment') === -1) {
// has to be after 'indented' check, since first chars of first indented line aren't marked as such
return false;
- } else if (firstTok.state.base.code &&
- (!line.styles || line.styles.length <= 2 || line.styles[2].indexOf('formatting-code-block') === -1)) {
+ } else if (firstTok.state.base.code && !fencing_line(line)) {
// have to look for the formatting-code-block style, since opening ``` has state.base.code set unlike all other fenced lines
return 'single';
} else {
@@ -155,48 +163,37 @@ $(window).load(function() {
cm.setSelection(cur_start, cur_end);
cm.focus();
} else if (is_code === 'fenced') {
- /*
- DEBUG
- */
- for (var i = 0; i < cm.lineCount(); i++) {
- tok0 = cm.getTokenAt({line: i, ch: 0});
- tok1 = cm.getTokenAt({line: i, ch: 1});
- //types0 = cm.getTokenTypeAt({line: i, ch:0});
- //types1 = cm.getTokenTypeAt({line: i, ch:1});
- line = cm.getLineHandle(i);
- //lineToks = cm.getLineTokens(i);
- //lastTok = lineToks.length && lineToks[lineToks.length-1]
- lastTok = line.text && cm.getTokenAt({line: i, ch: line.text.length-1});
- console.log('line', i,
- tok0.type, ',', tok1.type, ',',
- //tok0.state.base.indentedCode, tok1.state.base.indentedCode,
- 'last tok indented:', lastTok && lastTok.state.base.indentedCode,
- 'first tok indented:', tok1.state.base.indentedCode,
- 'last tok inline code:', lastTok && lastTok.state.base.code,
- 'first tok inline code:', tok1.state.base.code,
- 'last tok type:', lastTok && lastTok.type,
- 'styles:', line && line.styles, line && line.text);
- //console.log(cm.getLineTokens(i));
- }
- //return;
-
// FIXME what about selection?
// no selection, search for ends of this fenced block
- console.log('current line is', code_type(cm, cur_start.line));
-
- for (block_start = cur_start.line; block_start >= 0; block_start--) {
- line = cm.getLineHandle(block_start);
- if (line.styles && line.styles.length > 2 && line.styles[2].indexOf('formatting-code-block') !== -1) {
- break;
+ var search_from = cur_start.line;
+ console.log('fencing?', fencing_line(cm.getLineHandle(cur_start.line)), cur_start.line);
+ if (fencing_line(cm.getLineHandle(cur_start.line))) { // gets a little tricky if cursor is right on a fenced line
+ console.log(code_type(cm, cur_start.line+1));
+ if (code_type(cm, cur_start.line+1) === 'fenced') {
+ block_start = cur_start.line;
+ search_from = cur_start.line+1; // for searching for "end"
+ } else {
+ block_end = cur_start.line;
+ search_from = cur_start.line-1; // for searching for "start"
}
}
- lineCount = cm.lineCount();
- for (block_end = cur_start.line; block_end < lineCount; block_end++) {
- line = cm.getLineHandle(block_end);
- if (line.styles && line.styles.length > 2 && line.styles[2].indexOf('formatting-code-block') !== -1) {
- break;
+ if (block_start === undefined) {
+ for (block_start = search_from; block_start >= 0; block_start--) {
+ line = cm.getLineHandle(block_start);
+ if (fencing_line(line)) {
+ break;
+ }
+ }
+ }
+ if (block_end === undefined) {
+ lineCount = cm.lineCount();
+ for (block_end = search_from; block_end < lineCount; block_end++) {
+ line = cm.getLineHandle(block_end);
+ if (fencing_line(line)) {
+ break;
+ }
}
}
cm.operation(function() {