* Wraps each line of the string into tag with given style applied to it. * * @param {String} str Input string. * @param {String} css Style name to apply to the string. * @return {String} Returns input string with each line surrounded by tag.
(str, css)
| 781 | * @return {String} Returns input string with each line surrounded by <span/> tag. |
| 782 | */ |
| 783 | function wrapLinesWithCode(str, css) |
| 784 | { |
| 785 | if (str == null || str.length == 0 || str == '\n') |
| 786 | return str; |
| 787 | |
| 788 | str = str.replace(/</g, '<'); |
| 789 | |
| 790 | // Replace two or more sequential spaces with leaving last space untouched. |
| 791 | str = str.replace(/ {2,}/g, function(m) |
| 792 | { |
| 793 | var spaces = ''; |
| 794 | |
| 795 | for (var i = 0; i < m.length - 1; i++) |
| 796 | spaces += sh.config.space; |
| 797 | |
| 798 | return spaces + ' '; |
| 799 | }); |
| 800 | |
| 801 | // Split each line and apply <span class="...">...</span> to them so that |
| 802 | // leading spaces aren't included. |
| 803 | if (css != null) |
| 804 | str = eachLine(str, function(line) |
| 805 | { |
| 806 | if (line.length == 0) |
| 807 | return ''; |
| 808 | |
| 809 | var spaces = ''; |
| 810 | |
| 811 | line = line.replace(/^( | )+/, function(s) |
| 812 | { |
| 813 | spaces = s; |
| 814 | return ''; |
| 815 | }); |
| 816 | |
| 817 | if (line.length == 0) |
| 818 | return spaces; |
| 819 | |
| 820 | return spaces + '<code class="' + css + '">' + line + '</code>'; |
| 821 | }); |
| 822 | |
| 823 | return str; |
| 824 | }; |
| 825 | |
| 826 | /** |
| 827 | * Pads number with zeros until it's length is the same as given length. |