javascript - HTML字符串排版
迷茫
迷茫 2017-05-24 11:36:25
0
4
718

我现在有HTML代码的字符串,想要对字符串进行排版,有没有什么可用的方法?

需要一款js插件,或者方法,能在项目里面转换用的。

比如原字符串是:

<p><p>This is a p</p><p>This is another p</p></p>

添加空格和换行,变成新的字符串:

<p>
    <p>This is a p</p>
    <p>This is another p</p>
</p>
迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

全部回复(4)
世界只因有你

第一种方案想用正则做答没成功,没有成功。
整理了下思路,已经完美解决(自认为完美)。

function codeFormat(code, indent, tmpIndent){
    var indent = indent || '  ';
    var tmpIndent = tmpIndent || '\n';
    var preg = /<(\S*)([^>]*)>([\s\S]*?)<\/>/ig;
    return code.replace(preg, function(
/*
    感觉没什么难度,一个循环
    遇见 >后跟<,换行
    遇见 < 缩进
    遇见 </ 取消缩进
*/
function codeFormat(code, indent){
    var indent = indent || "  ";    //缩进字符
    var tmpIndent = "";    //保存代码字符串
    var result = "", key = "", keyNext = "";
    for( var i = 0 ; i < code.length ; i++ ){
        key = code[i];
        keyNext = i < code.length-1 ? code[i+1] : "";
        if(key == "<"){
            if( keyNext == "/" ){
                tmpIndent = tmpIndent.substr(indent.length);
            }
            if( result[result.length-1] == "\n" ){
                result += tmpIndent;
            }
            if( keyNext != "/" ){
                tmpIndent += indent;
            }
        }
        result += key;
        if(key == ">" && keyNext == "<" ){
            result += "\n";
        }
    }
    return result;
}

codeFormat("<p><p>This is a p</p><p>This is another p</p></p>");
/*
<p>
  <p>This is a p</p>
  <p>This is another p</p>
</p>
*/
codeFormat('<html><head><title></title></head><body class="haha"><p style="background:#C00;"><p>This is a p</p><p>This is another p</p></p></body></html>', '    ');
/*
<html>
    <head>
        <title>
        </title>
    </head>
    <body class="haha">
        <p style="background:#C00;">
            <p>This is a p</p>
            <p>This is another p</p>
        </p>
    </body>
</html>
*/
, , , ){ return tmpIndent + '<' + + + '>' + codeFormat(, indent, tmpIndent + indent) + ( .trim().substr(0,1) == '<' ? tmpIndent : '') + '</' + + '>'; }); } codeFormat("<p><p>This is a p</p><p>This is anothers p</p></p><p><p>This is a p</p><p>This is another p</p></p>"); /* <p> <p>This is a p</p> <p>This is anothers p</p> </p> <p> <p>This is a p</p> <p>This is another p</p> </p> */ codeFormat('<html><head><title></title></head><body class="haha"><p style="background:#C00;"><p>This is a p</p><p>This is another p</p></p></body></html>', '----'); /* <html> ----<head> --------<title></title> ----</head> ----<body class="haha"> --------<p style="background:#C00;"> ------------<p>This is a p</p> ------------<p>This is another p</p> --------</p> ----</body> </html> */

以下是循环的老答案:

rrreee
刘奇

为什么我这个问题一直被踩?还是一个不错的问题的嘛。。。。
@mqycn , @zhenguoli

这是朋友@candy写的一个方案:

import _ from 'lodash';

const splitOnTags = str => str.split(/(<\/?[^>]+>)/g).filter(line=>line.trim()!="");
const isTag = str => /<[^>!]+>/.test(str);
const isClosingTag = str => /<\/[^>]+>/.test(str);
const isSelfClosingTag = str => /<[^>]+\/>/.test(str);
const isOpeningTag = str => isTag(str) && !isClosingTag(str) && !isSelfClosingTag(str);

export default (str, indent) => {
  let depth = 0;
  indent = indent || '    ';

  return splitOnTags(str).map(item=>{
    if(isClosingTag(item)) {
      depth--;
    }

    const line = _.repeat(indent, depth) + item;

    if(isOpeningTag(item)) {
      depth++;
    } 

    return line;
  }).join('\n');
}
黄舟

http://tool.oschina.net/codef... 在线的。其他的编辑器里面有beautify

伊谢尔伦
  1. 对开发工具而言,你可以尝试 VSCode 编辑器。

https://code.visualstudio.com/

安装完成后,按 shift-ctrl-x 打开插件列表,搜索 Beautify 并安装,即可实现 HTML 代码美化。
相应设置参考这里:

https://marketplace.visualstu...

  1. 在代码中进行格式转换可以使用 HTML String Parser,例如

https://www.npmjs.com/package...

或者使用 cheerio 解析 DOM 文本后重新输出亦可。

3.(重新制作一个 HTML Parser 并不困难,参考我的专栏
http://ewind.us/2016/toy-html...

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!