2015-01-16 05:21:33 +08:00
|
|
|
/**
|
|
|
|
* Convert all tabs to spaces
|
|
|
|
*/
|
2017-01-30 03:28:30 +08:00
|
|
|
showdown.subParser('detab', function (text, options, globals) {
|
2015-01-19 19:37:21 +08:00
|
|
|
'use strict';
|
2017-01-30 03:28:30 +08:00
|
|
|
text = globals.converter._dispatch('detab.before', text, options, globals);
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2015-01-19 19:37:21 +08:00
|
|
|
// expand first n-1 tabs
|
|
|
|
text = text.replace(/\t(?=\t)/g, ' '); // g_tab_width
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2015-01-19 19:37:21 +08:00
|
|
|
// replace the nth with two sentinels
|
2017-01-29 08:07:19 +08:00
|
|
|
text = text.replace(/\t/g, '¨A¨B');
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2015-01-19 19:37:21 +08:00
|
|
|
// use the sentinel to anchor our regex so it doesn't explode
|
2017-01-29 08:07:19 +08:00
|
|
|
text = text.replace(/¨B(.+?)¨A/g, function (wholeMatch, m1) {
|
2015-01-19 20:04:22 +08:00
|
|
|
var leadingText = m1,
|
|
|
|
numSpaces = 4 - leadingText.length % 4; // g_tab_width
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2015-01-19 19:37:21 +08:00
|
|
|
// there *must* be a better way to do this:
|
|
|
|
for (var i = 0; i < numSpaces; i++) {
|
|
|
|
leadingText += ' ';
|
|
|
|
}
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2015-01-19 19:37:21 +08:00
|
|
|
return leadingText;
|
|
|
|
});
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2015-01-19 19:37:21 +08:00
|
|
|
// clean up sentinels
|
2017-01-29 08:07:19 +08:00
|
|
|
text = text.replace(/¨A/g, ' '); // g_tab_width
|
|
|
|
text = text.replace(/¨B/g, '');
|
2015-01-16 05:21:33 +08:00
|
|
|
|
2017-01-30 03:28:30 +08:00
|
|
|
text = globals.converter._dispatch('detab.after', text, options, globals);
|
2015-01-19 19:37:21 +08:00
|
|
|
return text;
|
2015-01-16 05:21:33 +08:00
|
|
|
});
|