1
Vote

A template for pasting code and cleaning unnecessary indenting

description

<%
string[] lines = code.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
string[] formattedLines = new string[lines.Length];
int indent = 0;
string firstLine = lines[0];
while (firstLine[indent] == ' ' || firstLine[indent] == '\t')
indent++;
for (int i = 0; i < lines.Length; i ++)
{
string line = lines[i];
if (line.Length > indent)
{
    int pos = 0;
    while (pos < indent && (line[pos] == ' ' || line[pos] == '\t') )
        pos ++;
    formattedLines[i] = line.Substring(pos, line.Length-pos);
}
else
{
    formattedLines[i] = line;
}
}
 
string formattedCode = string.Join(Environment.NewLine, formattedLines);
formattedCode = formattedCode.Replace(" ", " ").Replace("\t", " ");
 
%>
<pre name="code" class="brush: csharp"> <%= HtmlEncode(formattedCode) %> </pre>  
<p>Type here</p>

comments