detab.js 951 B

123456789101112131415161718192021222324252627282930313233
  1. /**
  2. * Convert all tabs to spaces
  3. */
  4. showdown.subParser('detab', function (text, options, globals) {
  5. 'use strict';
  6. text = globals.converter._dispatch('detab.before', text, options, globals);
  7. // expand first n-1 tabs
  8. text = text.replace(/\t(?=\t)/g, ' '); // g_tab_width
  9. // replace the nth with two sentinels
  10. text = text.replace(/\t/g, '¨A¨B');
  11. // use the sentinel to anchor our regex so it doesn't explode
  12. text = text.replace(/¨B(.+?)¨A/g, function (wholeMatch, m1) {
  13. var leadingText = m1,
  14. numSpaces = 4 - leadingText.length % 4; // g_tab_width
  15. // there *must* be a better way to do this:
  16. for (var i = 0; i < numSpaces; i++) {
  17. leadingText += ' ';
  18. }
  19. return leadingText;
  20. });
  21. // clean up sentinels
  22. text = text.replace(/¨A/g, ' '); // g_tab_width
  23. text = text.replace(/¨B/g, '');
  24. text = globals.converter._dispatch('detab.after', text, options, globals);
  25. return text;
  26. });