I am currently using the following script to convert indented plain text to HTML list code:
jQuery(function($) {
var indentedToHtmlList = function indentedToHtmlList (text, indentChar, folderChar, listType, showIcons) {
indentChar = indentChar || '\t';
folderChar = folderChar || ':';
listType = listType || 'ul';
showIcons = !!showIcons;
var lastDepth,
lines = text.split(/\r?\n/),
output = '<' + listType + '>\n',
depthCounter = new RegExp('^(' + indentChar + '*)(.*)');
for (var i = 0; i < lines.length; i++) {
var splitted = lines[i].match(depthCounter),
indentStr = splitted[1],
fileName = splitted[2],
currentDepth = (indentStr === undefined) ? 0 : (indentStr.length / indentChar.length),
isFolder = (fileName.charAt(fileName.length - 1) === folderChar);
if (isFolder) {
fileName = fileName.substring(0, fileName.length -1);
}
if (lastDepth === currentDepth) {
output += '</li>\n';
} else if (lastDepth > currentDepth) {
while (lastDepth > currentDepth) {
output += '</li>\n</' + listType + '>\n</li>\n';
lastDepth--;
}
} else if (lastDepth < currentDepth) {
output += '\n<' + listType + '>\n';
}
output += '<li>';
if (showIcons) {
output += '<span class=" glyphicon glyphicon-' +
(isFolder ? 'folder-open' : 'file') +
'"></span> ';
}
output += fileName;
lastDepth = currentDepth;
}
while (lastDepth >= 0) {
output += '\n</li>\n</' + listType + '>';
lastDepth--;
}
return output;
};
runConvert = function() {
var originalText = $('#textarea-plain-text').val(),
listType = $('#list-type').val(),
showIcons = !!$('#glyph-selector-box').prop('checked'),
result = indentedToHtmlList(originalText, '\t', ':', listType, showIcons);
$('#textarea-converted-text').val(result);
return $('#div-converted-text').html(result);
};
bind = function() {
return $('#list-conversion-button').click(runConvert);
};
$(bind);
});
The script outputs code which displays properly, which is part of the reason it took a while to notice that the syntax is off. Here is an example text, the converted version (i.e., the script's result), and a marked up version of the converted text showing where the errors are:
- Indented plain text sample.
- Script output.
- List of errors (as detected by BBEdit).
- Marked up output (errors commented and corrections added).
From what I can tell, it appears the script is inserting extraneous </li> tags and placing closing </ul> tags on nested lists too high in the hierarchy. Editing the first while loop seems to be the solution to the </li> issue, but I, and another developer, cannot figure out where the logic is going awry on the </ul>'s.
Here is a page which currently has the script implemented (so you can generate your own examples without creating a page to do so): Convert Indented/Nested Plain Text to an HTML List.
</li>closing tags at all. You do need the closing</ul>tags however.</li>so long as it is immediately followed by another<li>or there is no more content in the parent element: w3.org/TR/html-markup/li.html. Good to know.</ul>'s. I'm not actually attached to any particular way of accomplishing the end goal (i.e., converting indented plain text to a nested HTML list).