Comparing version 0.0.2 to 0.0.3
@@ -1,1 +0,1 @@ | ||
!function(t){"use strict";function e(t){null==t&&(t={}),this.settings={tabTagName:t.tabTagName||"span",tabClassName:t.tabClassName||"tab-char",tabSpaceMinWidth:t.tabSpaceMinWidth||"1em",styleId:t.styleId||"etab-style",styleRules:t.styleRules||[]}}function n(){i.processLines(t.querySelectorAll(".js-file-line"))}var s=" ",a=3;e.prototype._addStyle=function(t){if(!t.getElementById(this.settings.styleId)){var e=t.createElement("style");e.id=this.settings.styleId,t.body.appendChild(e);var n=this.settings.tabTagName+"."+this.settings.tabClassName;e.sheet.insertRule(n+"{ display: inline-block; margin-right: "+this.settings.tabSpaceMinWidth+" }",0),this.settings.styleRules.forEach(function(t,n){e.sheet.insertRule(t,n+1)})}},e.prototype.processLines=function(t){function e(t,i){if(!(t>=a.length)){if(i>=a[t].length)return e(t+1,0);var r=s(t,i);if(r.aligned)return e(t,i+1);n(r),setTimeout(function(){e(t,i+1)})}}function n(t){var e=t.map(function(t){return t.getBoundingClientRect().right}),n=Math.max.apply(null,e);t.forEach(function(t){t.style.width=n-t.getBoundingClientRect().right+"px"}),t.aligned=!0}function s(t,e){var n=[];if(t>=0&&t<a.length&&e>=0&&e<a[t].length){var s=l[t][e];if(s)return s;for(var i=t-1,r=t+1;i>=0&&e<a[i].length;)i--;for(;r<a.length&&e<a[r].length;)r++;for(var o=i+1;r>o;o++)n.push(a[o][e]),l[o][e]=n}return n}t.length&&this._addStyle(t[0].ownerDocument);for(var a=[],i=0,r=t.length;r>i;i++)this._wrapTabs(t[i]),a[i]=t[i].querySelectorAll(this.settings.tabTagName+"."+this.settings.tabClassName);var l=a.map(function(t){return new Array(t.length)});e(0,0)},e.prototype._wrapTabs=function(t){if(t.nodeType===a)for(var e;(e=t.wholeText.indexOf(s))>=0;){var n=t.splitText(e);t=n.splitText(1),this._wrapTab(n)}else if(!this._isTab(t))for(var i=t.firstChild;i;)i=this._wrapTabs(i).nextSibling;return t},e.prototype._wrapTab=function(t){var e=t.ownerDocument.createElement(this.settings.tabTagName);return e.classList.add(this.settings.tabClassName),t.parentNode.replaceChild(e,t),e.appendChild(t),e},e.prototype._isTab=function(t){return t.nodeName===this.settings.tabTagName&&t.classList.contains(this.settings.tabClassName)};var i=new e({styleRules:[".blob-code { font-family: 'Input Serif Narrow', 'Georgia', serif; font-size: 1.167em; }"]});$(t).ready(n).on("pjax:success",n)}(document); | ||
!function(t){"use strict";function e(t){var e=t||{};this.settings={tabTagName:e.tabTagName||"span",tabClassName:e.tabClassName||"tab-char",indentClassName:e.indentClassName||"ident",tabIndentExtraSpace:8,tabSpaceMinWidth:e.tabSpaceMinWidth||"1em",styleId:e.styleId||"etab-style",styleRules:e.styleRules||[]}}function n(){for(var e=/^["'([{“‘]+$/,n=t.querySelectorAll(".highlight .p"),s=0;s<n.length;s++)e.test(n[s].textContent)&&n[s].classList.add("open");for(var a=t.querySelectorAll(".blob-wrapper"),s=0;s<a.length;s++)r.processLines(a[s].querySelectorAll(".blob-code"))}var s=" ",a=" ",i=3;e.prototype._addStyle=function(t){if(!t.getElementById(this.settings.styleId)){var e=t.createElement("style");e.id=this.settings.styleId,t.body.appendChild(e);var n=this.settings.tabTagName+"."+this.settings.tabClassName;e.sheet.insertRule(n+"{ display: inline-block; margin-right: "+this.settings.tabSpaceMinWidth+" }",0),this.settings.styleRules.forEach(function(t,n){e.sheet.insertRule(t,n+1)})}},e.prototype.testLines=function(t){for(var e=/[^\t]+\t/,n=!1,s=0,i=t.length;i>s;s++){var r=t[s].textContent;if(r.charAt(0)===a)return!1;if(e.test(r)){if(n)return!0;n=!0}else n=!1}return!1},e.prototype.processLines=function(t){function e(t,i){if(!(t>=a.length)){if(i>=a[t].length)return e(t+1,0);var r=s(t,i);if(r.aligned)return e(t,i+1);n(r),setTimeout(function(){e(t,i+1)})}}function n(t){var e=t.map(function(t){var e=t.getBoundingClientRect().right;return t.classList.contains(o.indentClassName)&&(e+=o.tabIndentExtraSpace),e}),n=Math.max.apply(null,e);t.forEach(function(t){t.style.width=n-t.getBoundingClientRect().right+"px"}),t.aligned=!0}function s(t,e){var n=[];if(t>=0&&t<a.length&&e>=0&&e<a[t].length){var s=l[t][e];if(s)return s;for(var i=t-1,r=t+1;i>=0&&e<a[i].length;)i--;for(;r<a.length&&e<a[r].length;)r++;for(var o=i+1;r>o;o++)n.push(a[o][e]),l[o][e]=n}return n}if(this.testLines(t)){this._addStyle(t[0].ownerDocument);for(var a=[],i=0,r=t.length;r>i;i++)this._wrapAllTabs(t[i]),a[i]=t[i].querySelectorAll(this.settings.tabTagName+"."+this.settings.tabClassName);var l=a.map(function(t){return new Array(t.length)}),o=this.settings;e(0,0)}},e.prototype._wrapAllTabs=function(t,e){if(void 0===e&&(e=!0),t.nodeType===i){for(var n;(n=t.wholeText.indexOf(s))>=0;){e&&n>0&&(e=!1);var a=t.splitText(n);t=a.splitText(1),this._wrapTab(a,e)}e&&t.wholeText.length>0&&(e=!1)}else if(!this._isTab(t))for(var r=t.firstChild;r;){var l=r.nextSibling;e=this._wrapAllTabs(r,e),r=l}return e},e.prototype._wrapTab=function(t,e){var n=t.ownerDocument.createElement(this.settings.tabTagName);return n.classList.add(this.settings.tabClassName),e&&n.classList.add(this.settings.indentClassName),t.parentNode.replaceChild(n,t),n.appendChild(t),n},e.prototype._isTab=function(t){return t.nodeName===this.settings.tabTagName&&t.classList.contains(this.settings.tabClassName)},e.prototype._wrapTabColumns=function(){};var r=new e({styleRules:[".blob-code { font-family: 'Input Serif Narrow', 'Georgia', serif; font-size: 1.167em; }",".highlight .p { color: #bbb; font-weight: lighter; }",".highlight span.tab-char + .open.p { position: absolute; transform: translateX(-100%); }"]});$(t).ready(n).on("pjax:success",n)}(document); |
// ==UserScript== | ||
// @name elastic tabstops view for github | ||
// @namespace https://github.com/hax/ | ||
// @version 0.2 | ||
// @description | ||
// @match https://github.com/* | ||
// @copyright 2014, Hax | ||
// @name github-etab | ||
// @namespace https://github.com/hax/etab | ||
// @version 0.7 | ||
// @description Support elastic tabstops for github.com | ||
// @match https://github.com/* | ||
// @updateURL https://raw.githubusercontent.com/hax/etab/master/dist/github.meta.js | ||
// @downloadURL https://raw.githubusercontent.com/hax/etab/master/dist/github.user.js | ||
// @copyright 2014, Hax | ||
// ==/UserScript== | ||
!function(t){"use strict";function e(t){null==t&&(t={}),this.settings={tabTagName:t.tabTagName||"span",tabClassName:t.tabClassName||"tab-char",tabSpaceMinWidth:t.tabSpaceMinWidth||"1em",styleId:t.styleId||"etab-style",styleRules:t.styleRules||[]}}function n(){i.processLines(t.querySelectorAll(".js-file-line"))}var s=" ",a=3;e.prototype._addStyle=function(t){if(!t.getElementById(this.settings.styleId)){var e=t.createElement("style");e.id=this.settings.styleId,t.body.appendChild(e);var n=this.settings.tabTagName+"."+this.settings.tabClassName;e.sheet.insertRule(n+"{ display: inline-block; margin-right: "+this.settings.tabSpaceMinWidth+" }",0),this.settings.styleRules.forEach(function(t,n){e.sheet.insertRule(t,n+1)})}},e.prototype.processLines=function(t){function e(t,i){if(!(t>=a.length)){if(i>=a[t].length)return e(t+1,0);var r=s(t,i);if(r.aligned)return e(t,i+1);n(r),setTimeout(function(){e(t,i+1)})}}function n(t){var e=t.map(function(t){return t.getBoundingClientRect().right}),n=Math.max.apply(null,e);t.forEach(function(t){t.style.width=n-t.getBoundingClientRect().right+"px"}),t.aligned=!0}function s(t,e){var n=[];if(t>=0&&t<a.length&&e>=0&&e<a[t].length){var s=l[t][e];if(s)return s;for(var i=t-1,r=t+1;i>=0&&e<a[i].length;)i--;for(;r<a.length&&e<a[r].length;)r++;for(var o=i+1;r>o;o++)n.push(a[o][e]),l[o][e]=n}return n}t.length&&this._addStyle(t[0].ownerDocument);for(var a=[],i=0,r=t.length;r>i;i++)this._wrapTabs(t[i]),a[i]=t[i].querySelectorAll(this.settings.tabTagName+"."+this.settings.tabClassName);var l=a.map(function(t){return new Array(t.length)});e(0,0)},e.prototype._wrapTabs=function(t){if(t.nodeType===a)for(var e;(e=t.wholeText.indexOf(s))>=0;){var n=t.splitText(e);t=n.splitText(1),this._wrapTab(n)}else if(!this._isTab(t))for(var i=t.firstChild;i;)i=this._wrapTabs(i).nextSibling;return t},e.prototype._wrapTab=function(t){var e=t.ownerDocument.createElement(this.settings.tabTagName);return e.classList.add(this.settings.tabClassName),t.parentNode.replaceChild(e,t),e.appendChild(t),e},e.prototype._isTab=function(t){return t.nodeName===this.settings.tabTagName&&t.classList.contains(this.settings.tabClassName)};var i=new e({styleRules:[".blob-code { font-family: 'Input Serif Narrow', 'Georgia', serif; font-size: 1.167em; }"]});$(t).ready(n).on("pjax:success",n)}(document); | ||
!function(t){"use strict";function e(t){var e=t||{};this.settings={tabTagName:e.tabTagName||"span",tabClassName:e.tabClassName||"tab-char",indentClassName:e.indentClassName||"ident",tabIndentExtraSpace:8,tabSpaceMinWidth:e.tabSpaceMinWidth||"1em",styleId:e.styleId||"etab-style",styleRules:e.styleRules||[]}}function n(){for(var e=/^["'([{“‘]+$/,n=t.querySelectorAll(".highlight .p"),s=0;s<n.length;s++)e.test(n[s].textContent)&&n[s].classList.add("open");for(var a=t.querySelectorAll(".blob-wrapper"),s=0;s<a.length;s++)r.processLines(a[s].querySelectorAll(".blob-code"))}var s=" ",a=" ",i=3;e.prototype._addStyle=function(t){if(!t.getElementById(this.settings.styleId)){var e=t.createElement("style");e.id=this.settings.styleId,t.body.appendChild(e);var n=this.settings.tabTagName+"."+this.settings.tabClassName;e.sheet.insertRule(n+"{ display: inline-block; margin-right: "+this.settings.tabSpaceMinWidth+" }",0),this.settings.styleRules.forEach(function(t,n){e.sheet.insertRule(t,n+1)})}},e.prototype.testLines=function(t){for(var e=/[^\t]+\t/,n=!1,s=0,i=t.length;i>s;s++){var r=t[s].textContent;if(r.charAt(0)===a)return!1;if(e.test(r)){if(n)return!0;n=!0}else n=!1}return!1},e.prototype.processLines=function(t){function e(t,i){if(!(t>=a.length)){if(i>=a[t].length)return e(t+1,0);var r=s(t,i);if(r.aligned)return e(t,i+1);n(r),setTimeout(function(){e(t,i+1)})}}function n(t){var e=t.map(function(t){var e=t.getBoundingClientRect().right;return t.classList.contains(o.indentClassName)&&(e+=o.tabIndentExtraSpace),e}),n=Math.max.apply(null,e);t.forEach(function(t){t.style.width=n-t.getBoundingClientRect().right+"px"}),t.aligned=!0}function s(t,e){var n=[];if(t>=0&&t<a.length&&e>=0&&e<a[t].length){var s=l[t][e];if(s)return s;for(var i=t-1,r=t+1;i>=0&&e<a[i].length;)i--;for(;r<a.length&&e<a[r].length;)r++;for(var o=i+1;r>o;o++)n.push(a[o][e]),l[o][e]=n}return n}if(this.testLines(t)){this._addStyle(t[0].ownerDocument);for(var a=[],i=0,r=t.length;r>i;i++)this._wrapAllTabs(t[i]),a[i]=t[i].querySelectorAll(this.settings.tabTagName+"."+this.settings.tabClassName);var l=a.map(function(t){return new Array(t.length)}),o=this.settings;e(0,0)}},e.prototype._wrapAllTabs=function(t,e){if(void 0===e&&(e=!0),t.nodeType===i){for(var n;(n=t.wholeText.indexOf(s))>=0;){e&&n>0&&(e=!1);var a=t.splitText(n);t=a.splitText(1),this._wrapTab(a,e)}e&&t.wholeText.length>0&&(e=!1)}else if(!this._isTab(t))for(var r=t.firstChild;r;){var l=r.nextSibling;e=this._wrapAllTabs(r,e),r=l}return e},e.prototype._wrapTab=function(t,e){var n=t.ownerDocument.createElement(this.settings.tabTagName);return n.classList.add(this.settings.tabClassName),e&&n.classList.add(this.settings.indentClassName),t.parentNode.replaceChild(n,t),n.appendChild(t),n},e.prototype._isTab=function(t){return t.nodeName===this.settings.tabTagName&&t.classList.contains(this.settings.tabClassName)},e.prototype._wrapTabColumns=function(){};var r=new e({styleRules:[".blob-code { font-family: 'Input Serif Narrow', 'Georgia', serif; font-size: 1.167em; }",".highlight .p { color: #bbb; font-weight: lighter; }",".highlight span.tab-char + .open.p { position: absolute; transform: translateX(-100%); }"]});$(t).ready(n).on("pjax:success",n)}(document); |
{ | ||
"name": "etab", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"description": "", | ||
@@ -11,3 +11,3 @@ "main": "index.js", | ||
"test": "echo \"Error: no test specified\" && exit 1", | ||
"dist": "uglifyjs src/index.js src/github.js -o dist/github.js -e document:document -mc --inline-script && uglifyjs --comments all src/github-head.js dist/github.js -o dist/github.user.js", | ||
"dist": "cp src/github.meta.js dist; uglifyjs src/index.js src/github.js -o dist/github.js -e document:document -mc --inline-script && uglifyjs --comments all dist/github.meta.js dist/github.js -o dist/github.user.js", | ||
"prepublish": "npm run dist" | ||
@@ -14,0 +14,0 @@ }, |
@@ -1,2 +0,2 @@ | ||
# Elastic Tabstops JavaScript Implementation | ||
# Elastic Tabstops JavaScript Implementation for Browsers | ||
@@ -9,2 +9,34 @@ | ||
etab.processLines(document.querySelectorAll('.code-line')) | ||
``` | ||
``` | ||
## Examples | ||
Here is the [user script for supporting elastic tabstops on github](https://github.com/hax/etab/blob/master/src/github.js) (note currently only tested under [tempermonkey](http://tampermonkey.net/)). | ||
After [installing it](https://github.com/hax/etab/raw/master/dist/github.user.js), you could try some samples in your browser: | ||
* https://github.com/hax/etab/blob/master/src/index.js | ||
* https://github.com/hax/etab/blob/master/src/github-head.js | ||
* https://github.com/hax/haojing/blob/elastic-tabstops/htdocs/index.php | ||
* https://github.com/hax/etab/blob/master/example/hanging-punc.js | ||
## TODO | ||
### Core | ||
* [ ] try to utilize CSS tab-size | ||
* [ ] indent width (normally wide than tab min width for align) | ||
* [ ] detect tab size of the source | ||
* [ ] extension of alignment: | ||
- `auto aligned[TAB]` | ||
- `[SP]right aligned[TAB]` | ||
- `left aligned[SP][TAB]` | ||
- `[SP]center aligned[SP][TAB]` | ||
- `3.14159 (decimal aligned)[TAB]` | ||
### Github user script | ||
- [x] test Firefox (with Geasemonkey) | ||
- [ ] test IE (with ?) | ||
- [ ] add settings button and dialog | ||
- [ ] allow change font, tab width and other configurations | ||
- [x] support diff view | ||
- [ ] only process the code block that use elastic tabstops |
var etab = new ElasticTabstops({ | ||
styleRules: [ | ||
".blob-code { font-family: 'Input Serif Narrow', 'Georgia', serif; font-size: 1.167em; }", | ||
".highlight .p { color: #bbb; font-weight: lighter; }", | ||
".highlight span.tab-char + .open.p { position: absolute; transform: translateX(-100%); }", | ||
] | ||
@@ -8,5 +10,14 @@ }) | ||
function process() { | ||
etab.processLines(document.querySelectorAll('.js-file-line')) | ||
var openPuncs = /^["'([{“‘]+$/ | ||
var puncs = document.querySelectorAll('.highlight .p') | ||
for (var i = 0; i < puncs.length; i++) { | ||
if (openPuncs.test(puncs[i].textContent)) puncs[i].classList.add('open') | ||
} | ||
var blobs = document.querySelectorAll('.blob-wrapper') | ||
for (var i = 0; i < blobs.length; i++) { | ||
etab.processLines(blobs[i].querySelectorAll('.blob-code')) | ||
} | ||
} | ||
$(document).ready(process).on('pjax:success', process) |
'use strict' | ||
var TAB = '\t' | ||
var SPACE = ' ' | ||
var TEXT_NODE = 3 | ||
function ElasticTabstops(settings) { | ||
if (settings == null) settings = {} | ||
var s = settings || {} | ||
this.settings = { | ||
tabTagName : settings.tabTagName || 'span', | ||
tabClassName : settings.tabClassName || 'tab-char', | ||
//tabIndentWidth : settings.tabIndentWidth || '1.5em', | ||
tabSpaceMinWidth : settings.tabSpaceMinWidth || '1em', | ||
styleId : settings.styleId || 'etab-style', | ||
styleRules : settings.styleRules || [], | ||
//openPunctuations : '"\'([{“‘', //Unicode categories Ps, Pf, Pi | ||
//hangingPunctutaion : true, | ||
tabTagName : s.tabTagName || 'span', | ||
tabClassName : s.tabClassName || 'tab-char', | ||
//tabIndentWidth : s.tabIndentWidth || '1.5em', | ||
indentClassName : s.indentClassName || 'ident', | ||
tabIndentExtraSpace : 8, | ||
tabSpaceMinWidth : s.tabSpaceMinWidth || '1em', | ||
styleId : s.styleId || 'etab-style', | ||
styleRules : s.styleRules || [], | ||
//openPunctuations : s.openPunctuations || '"\'([{“‘', // Unicode categories Ps, Pf, Pi | ||
//hangingPunctutaion : s.hangingPunctutaion !== undefined ? !!s.hangingPunctutaion : true, | ||
//openClassName : s.openClassName || 'open', | ||
} | ||
@@ -32,9 +36,27 @@ } | ||
ElasticTabstops.prototype.testLines = function (lineNodes) { | ||
var etabPattern = /[^\t]+\t/ | ||
var state = false | ||
for (var i = 0, n = lineNodes.length; i < n; i++) { | ||
var text = lineNodes[i].textContent | ||
if (text.charAt(0) === SPACE) return false | ||
if (etabPattern.test(text)) { | ||
if (state) return true | ||
else state = true | ||
} else { | ||
state = false | ||
} | ||
} | ||
return false | ||
} | ||
ElasticTabstops.prototype.processLines = function (lineNodes) { | ||
if (lineNodes.length) this._addStyle(lineNodes[0].ownerDocument) | ||
if (!this.testLines(lineNodes)) return | ||
this._addStyle(lineNodes[0].ownerDocument) | ||
var lines = [] | ||
for (var i = 0, n = lineNodes.length; i < n; i++) { | ||
this._wrapTabs(lineNodes[i]) | ||
this._wrapAllTabs(lineNodes[i]) | ||
lines[i] = lineNodes[i].querySelectorAll(this.settings.tabTagName + '.' + this.settings.tabClassName) | ||
@@ -45,2 +67,4 @@ } | ||
var settings = this.settings | ||
alignNext(0, 0) | ||
@@ -61,3 +85,5 @@ | ||
var rights = cells.map(function (x) { | ||
return x.getBoundingClientRect().right | ||
var r = x.getBoundingClientRect().right | ||
if (x.classList.contains(settings.indentClassName)) r += settings.tabIndentExtraSpace | ||
return r | ||
}) | ||
@@ -88,21 +114,28 @@ var rightmost = Math.max.apply(null, rights) | ||
ElasticTabstops.prototype._wrapTabs = function wrapTabs(domNode) { | ||
ElasticTabstops.prototype._wrapAllTabs = function wrapTabs(domNode, indent) { | ||
if (indent === undefined) indent = true | ||
if (domNode.nodeType === TEXT_NODE) { | ||
var i | ||
while ((i = domNode.wholeText.indexOf(TAB)) >= 0) { | ||
if (indent && i > 0) indent = false | ||
var t = domNode.splitText(i) | ||
domNode = t.splitText(1) | ||
this._wrapTab(t) | ||
//domNode = t | ||
this._wrapTab(t, indent) | ||
} | ||
if (indent && domNode.wholeText.length > 0) indent = false | ||
} else if (!this._isTab(domNode)) { | ||
var node = domNode.firstChild | ||
while (node) node = this._wrapTabs(node).nextSibling | ||
while (node) { | ||
var next = node.nextSibling | ||
indent = this._wrapAllTabs(node, indent) | ||
node = next | ||
} | ||
} | ||
return domNode | ||
return indent | ||
} | ||
ElasticTabstops.prototype._wrapTab = function wrapTab(tab) { | ||
ElasticTabstops.prototype._wrapTab = function wrapTab(tab, indent) { | ||
var e = tab.ownerDocument.createElement(this.settings.tabTagName) | ||
e.classList.add(this.settings.tabClassName) | ||
if (indent) e.classList.add(this.settings.indentClassName) | ||
tab.parentNode.replaceChild(e, tab) | ||
@@ -117,1 +150,5 @@ e.appendChild(tab) | ||
} | ||
ElasticTabstops.prototype._wrapTabColumns = function wrapTabColumns(domNode) { | ||
//todo | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
1108185
18
4234
42
0