简体   繁体   English

从pre / code标签中的缩进HTML源中删除前导空格

[英]Removing leading whitespace from indented HTML source in pre/code tags

I currently have the following html within a pre-code block: 我目前在预代码块中有以下html:

                <pre class="prettyprint"><code>
                    &lt;html&gt;
                    &lt;body&gt;

                    &lt;form name=&quot;input&quot; action=&quot;html_form_action.asp&quot; method=&quot;get&quot;&gt;
                    &lt;input type=&quot;radio&quot; name=&quot;sex&quot; value=&quot;male&quot;&gt;Male&lt;br&gt;
                    &lt;input type=&quot;radio&quot; name=&quot;sex&quot; value=&quot;female&quot;&gt;Female&lt;br&gt;
                    &lt;input type=&quot;submit&quot; value=&quot;Submit&quot;&gt;
                    &lt;/form&gt; 

                    &lt;p&gt;If you click the &quot;Submit&quot; button, the form-data will be sent to a page called &quot;html_form_action.asp&quot;.&lt;/p&gt;

                    &lt;/body&gt;
                    &lt;/html&gt;
                </code></pre>

It is indented within the html source for better structure within the document. 它在html源代码中缩进,以便在文档中获得更好的结构。 How can I remove the leading whitespace? 如何删除前导空格? Through the use of javascript or is there a more simple method. 通过使用javascript还是有一个更简单的方法。

The question asks if there's a JavaScript solution or a simpler method for removing leading whitespace. 问题是询问是否有JavaScript解决方案或更简单的方法来删除前导空格。 There's a simpler method: 有一个更简单的方法:

CSS CSS

pre, code {
    white-space: pre-line;
}

DEMO DEMO

white-space 空白

The white-space property is used to describe how whitespace inside the element is handled. white-space属性用于描述如何处理元素内的空白。

pre-line 前行

Sequences of whitespace are collapsed. 空白的序列被折叠。

I really like Homam's idea, but I had to change it to deal with this: 我真的很喜欢Homam的想法,但我不得不改变它来处理这个问题:

<pre><code><!-- There's nothing on this line, so the script thinks the indentation is zero -->
    foo = bar
</code></pre>

To fix it, I just take out the first line if it's empty: 要解决这个问题,如果它是空的,我只需取出第一行:

[].forEach.call(document.querySelectorAll('code'), function($code) {
    var lines = $code.textContent.split('\n');

    if (lines[0] === '')
    {
        lines.shift()
    }

    var matches;
    var indentation = (matches = /^[\s\t]+/.exec(lines[0])) !== null ? matches[0] : null;
    if (!!indentation) {
        lines = lines.map(function(line) {
            line = line.replace(indentation, '')
            return line.replace(/\t/g, '    ')
        });

        $code.textContent = lines.join('\n').trim();
    }
});

(I'm also processing <code> tags instead of <pre> tags.) (我也在处理<code>标签而不是<pre>标签。)

You may want to just change how it is output, but it is fairly simple to do with JavaScript 您可能只想更改输出方式,但使用JavaScript非常简单

var p = document.querySelector(".prettyprint");
p.textContent = p.textContent.replace(/^\s+/mg, "");

http://jsfiddle.net/a4gfZ/ http://jsfiddle.net/a4gfZ/

Extending the above solution, this snippet assumes the indentation of the the first line inside <pre> is 0 and it realigns all the lines based on the first line: 扩展上面的解决方案,这段代码假设<pre>内第一行的缩进为0,它根据第一行重新排列所有行:

[].forEach.call(document.querySelectorAll('pre'), function($pre) {
  var lines = $pre.textContent.split('\n');
  var matches;
  var indentation = (matches = /^\s+/.exec(lines[0])) != null ? matches[0] : null;
  if (!!indentation) {
    lines = lines.map(function(line) {
      return line.replace(indentation, '');
    });
    return $pre.textContent = lines.join('\n').trim();
  }
});

When you use pre , you should format its content to be exactly as you want it to be rendered. 当您使用pre ,您应该将其内容格式化为您想要呈现的内容。 That's the very idea of pre (preformatted text). 这就是pre (预先格式化的文本)的想法。 But if it's just indentation, you could use CSS: margin-left with a suitable negative value. 但如果它只是缩进,你可以使用CSS: margin-left和一个合适的负值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM