Sublime Text 2:将HTML标签的内容格式化为一行(Sublime Text 2: Format HTML tags' content in one line)

编程入门 行业动态 更新时间:2024-10-26 14:28:35
Sublime Text 2:将HTML标签的内容格式化为一行(Sublime Text 2: Format HTML tags' content in one line)

好的,我的情况与这里关于格式化HTML的所有线程有点不同。 此外,我已经安装了大约每个HTML格式化插件,结果不一致,没有一个能真正解决我的问题。

此外,配置这些插件可能很困难,因为我不是程序员,有些作者没有提供足够的信息来定制他们的插件。

目前唯一可以格式化HTML的插件就是HTMLTidy ,问题是每个标签使用的空间太多。 请参阅下面的HTML示例。

我也尝试了他们在这个帖子中提到的内容,它只在某种程度上适用于缩进,但不是真正用于“真正的”标记格式化。

我想做的就是转动这个标记:

<ul> <li> item here </li> <li> item here </li> <li> item here </li> </ul>

或者这一个:

<p> Content... </p>

进入这个:

<ul> <li>item here</li> <li>item here</li> <li>item here</li> </ul>

和这个:

<p>Content...</p>

将每个标签的内容与开/关标签分开,这对我来说是浪费空间。

任何想法如何通过修改TidyHTML中的东西或手动,或任何其他方式来实现这一点?

提前致谢。

Ok, my situation is a bit different than all the threads here about formatting HTML. Also, I've installed about every HTML formatting plugin out there with mixed results and none really solve my issue.

Also, configuring these plugins can be difficult since I'm not a programmer and some authors don't provide enough information to customize their plugins.

For now the only plugin that formats my HTML somewhat Ok is HTMLTidy, the problem is that it uses too much space per tag. See my HTML example below.

I also tried what they mentioned in this thread and it only works to some extent as far as indentation goes, but not really for 'true' markup formatting.

All I want to do is turn this markup:

<ul> <li> item here </li> <li> item here </li> <li> item here </li> </ul>

Or this one:

<p> Content... </p>

Into this:

<ul> <li>item here</li> <li>item here</li> <li>item here</li> </ul>

And this:

<p>Content...</p>

Having the content of every tag in a separate line from the opening/closing tags is a waste of space for me.

Any idea how to accomplish this either by modifying something in TidyHTML or manually, or any other way for that matter?

Thanks in advance.

最满意答案

注意:此解决方案仅适用于ST2,因为此时(2014年7月31日)HTMLTidy不适用于ST3。

我想出了一个临时的解决方案,无论如何都不理想,但......

转至: Preferences -> Key Bindings - User 。 您将编辑文件Default(Windows).sublime-keypmap

将这两个指令添加到该文件中(请注意,指令必须位于[]括号内):

[ { "keys": ["ctrl+shift+r"], "command": "reindent" , "args": {"single_line": false}}, //Indent code -- http://stackoverflow.com/questions/8839753/formatting-html-code-using-sublime-text-2 { "keys": ["ctrl+alt+t"], "command": "html_tidy"} //HTMLTidy key binding ]

保存文件。

确保已安装HTMLTidy 。 打开HTMLTidy的设置文件HtmlTidy.sublime-settings : Preferences -> Package Settings -> HtmlTidy -> Settings - Default 。

查找"indent": true, (包括双引号和逗号)并将其更改为false 。

保存文件。

返回HTML文件,然后在一行中选择要格式化的代码部分 。 是的,如果您选择整个标记o_O,它将不起作用。 它只会留下<body>标签内的内容,并且会删除其他所有内容( <html> , <head>等)。

CTRL + ALT + T执行TidyHTML。 这将缩小您的标记并将标记放在一行中。

最后按CTRL + SHIFT + R执行缩进。

祝你好运。

Note: This solution works only in ST2, because at this moment (7/31/14) HTMLTidy is not available for ST3.

I figured a temporary solution, not ideal by any means, but...

Go to: Preferences -> Key Bindings - User. You will be editing the file Default (Windows).sublime-keypmap.

Add these two instructions to that file (note that the instructions need to be inside the [ ] brackets):

[ { "keys": ["ctrl+shift+r"], "command": "reindent" , "args": {"single_line": false}}, //Indent code -- http://stackoverflow.com/questions/8839753/formatting-html-code-using-sublime-text-2 { "keys": ["ctrl+alt+t"], "command": "html_tidy"} //HTMLTidy key binding ]

Save the file.

Make sure you have HTMLTidy installed. Open HTMLTidy's Settings file HtmlTidy.sublime-settings: Preferences -> Package Settings -> HtmlTidy -> Settings - Default.

Look for "indent": true, (include double quotes and comma) and change it to false.

Save the file.

Go back to your HTML file and select a section of the code you want to format in one line. Yes, it doesn't work if you select the entire markup o_O. It will only leave what's inside the <body> tag, and it will delete everything else (<html>, <head>, etc).

Press CTRL+ALT+T to execute TidyHTML. This will shrink your markup and make the tags in one line.

Finally press CTRL+SHIFT+R to execute the indentation.

Good luck.

更多推荐

本文发布于:2023-08-07 08:01:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1462582.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:标签   格式   内容   HTML   Sublime

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!