sublime text 3 插件之 HTML-CSS-JS Prettify 格式化美化代码

发布于 2021-11-28 09:47:14 字数 18743 浏览 1571 评论 0

1、插件介绍

这是一款集成了格式化(美化)html、css、js 三种文件类型的插件。插件依赖于 nodejs,因此需要事先安装 nodejs,然后才可以正常运行。

img.png

插件安装完成后,快捷键 ctrl+shift+H 完成当前文件的美化操作。插件对 html、css 文件的美化不是非常满意,但还可以,后面将说明如何修改 css 美化脚本,此为后话。

2、插件配置

下面这是我的插件配置,主要修改了缩进符为制表符。

{
  // Details: https://github.com/victorporof/Sublime-HTMLPrettify#using-your-own-jsbeautifyrc-options
  // Documentation: https://github.com/einars/js-beautify/
  "html": {
    "brace_style": "collapse", // "expand", "end-expand", "expand-strict"
    "indent_char": "\t",
    "indent_scripts": "keep", // "separate", "normal"
    "indent_size": 1,
    "max_preserve_newlines": 10,
    "preserve_newlines": true,
    "unformatted": ["pre", "code", "textarea", "title"],
    "wrap_line_length": 0
  },
  "css": {
    "indent_char": "\t",
    "indent_size": 1
  },
  "js": {
    "brace_style": "collapse", // "expand", "end-expand", "expand-strict"
    "break_chained_methods": false,
    "e4x": false,
    "eval_code": false,
    "indent_char": "\t",
    "indent_level": 0,
    "indent_size": 1,
    "indent_with_tabs": true,
    "jslint_happy": true,
    "keep_array_indentation": false,
    "keep_function_indentation": false,
    "max_preserve_newlines": 10,
    "preserve_newlines": true,
    "space_before_conditional": true,
    "space_in_paren": false,
    "unescape_strings": false,
    "wrap_line_length": 0
  }
}

3、插件修改

插件对 css 的美化不尽如人意,会导致 css 注释的下一行有个空格符,并且删除了本该有的换行。

修改 css 美化脚本,从菜单->Preferences->Browse Packages,打开 HTML->CSS-JS Prettify/scripts/beautify-css.js文件,修改如下:

/*!
 * @author Ariya Hidayat.
 * @link http://cssbeautify.com/
 * @author ydr.me
 * @link /post/sublime-text-3-plugin-html-css-js-prettify.html
 * @time 2014年3月25日14:25:03
 * 修改以适配 sublime text 3 html-css-js-prettify 插件
 */
 
/*
 Copyright (C) 2013 Sencha Inc.
 Copyright (C) 2012 Sencha Inc.
 Copyright (C) 2011 Sencha Inc.
 Author: Ariya Hidayat.
 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:
 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
*/
/*jslint continue: true, indent: 4 */
/*global exports:true, module:true, window:true */
 
(function () {
    'use strict';
 
    function cssbeautify(style, opt) {
        var options, index = 0,
            length = style.length,
            blocks, formatted = '',
            ch, ch2, str, state, State, depth, quote, comment,
            openbracesuffix = true,
            autosemicolon = false,
            trimRight,
            // 记录第一个字符,以适配于html中的style标签里的样式美化
            firstChar = '';
 
        options = arguments.length > 1 ? opt : {};
        if (options.indent === undefined) {
            options.indent = '';
            options.indent_size = options.indent_size || 1;
            options.indent_char = options.indent_char || '\t';
            while (options.indent_size--) {
                options.indent += options.indent_char;
            }
        }
        if (typeof options.indent === 'undefined') {
            options.indent = ' ';
        }
        if (typeof options.openbrace === 'string') {
            openbracesuffix = (options.openbrace === 'end-of-line');
        }
        if (typeof options.autosemicolon === 'boolean') {
            autosemicolon = options.autosemicolon;
        }
 
        function isWhitespace(c) {
            return (c === ' ') || (c === '\n') || (c === '\t') || (c === '\r') || (c === '\f');
        }
 
        function isQuote(c) {
            return (c === '\'') || (c === '"');
        }
        // FIXME: handle Unicode characters
 
        function isName(c) {
            return (ch >= 'a' && ch <= 'z') ||
                (ch >= 'A' && ch <= 'Z') ||
                (ch >= '0' && ch <= '9') ||
                '-_*.:#[]'.indexOf(c) >= 0;
        }
 
        function appendIndent() {
            var i;
            for (i = depth; i > 0; i -= 1) {
                formatted += options.indent;
            }
        }
 
        function openBlock() {
            formatted = trimRight(formatted);
            if (openbracesuffix) {
                formatted += ' {';
            } else {
                formatted += '\n';
                appendIndent();
                formatted += '{';
            }
            if (ch2 !== '\n') {
                formatted += '\n';
            }
            depth += 1;
        }
 
        function closeBlock() {
            var last;
            depth -= 1;
            formatted = trimRight(formatted);
            if (formatted.length > 0 && autosemicolon) {
                last = formatted.charAt(formatted.length - 1);
                if (last !== ';' && last !== '{') {
                    formatted += ';';
                }
            }
            formatted += '\n';
            appendIndent();
            formatted += '}';
            blocks.push(formatted);
            formatted = '';
        }
        if (String.prototype.trimRight) {
            trimRight = function (s) {
                return s.trimRight();
            };
        } else {
            // old Internet Explorer
            trimRight = function (s) {
                return s.replace(/\s+$/, '');
            };
        }
        State = {
            Start: 0,
            AtRule: 1,
            Block: 2,
            Selector: 3,
            Ruleset: 4,
            Property: 5,
            Separator: 6,
            Expression: 7,
            URL: 8
        };
        depth = 0;
        state = State.Start;
        comment = false;
        blocks = [];
        // We want to deal with LF (\n) only
        style = style.replace(/\r\n/g, '\n');
 
        firstChar = (style.match(/^[ \t]+/) || [''])[0]
 
        while (index < length) {
            ch = style.charAt(index);
            ch2 = style.charAt(index + 1);
            index += 1;
            // Inside a string literal?
            if (isQuote(quote)) {
                formatted += ch;
                if (ch === quote) {
                    quote = null;
                }
                if (ch === '\\' && ch2 === quote) {
                    // Don't treat escaped character as the closing quote
                    formatted += ch2;
                    index += 1;
                }
                continue;
            }
            // Starting a string literal?
            if (isQuote(ch)) {
                formatted += ch;
                quote = ch;
                continue;
            }
            // Comment
            if (comment) {
                formatted += ch;
                if (ch === '*' && ch2 === '/') {
                    comment = false;
                    formatted += ch2;
                    index += 1;
                }
                continue;
            }
            if (ch === '/' && ch2 === '*') {
                comment = true;
                formatted += ch;
                formatted += ch2;
                index += 1;
                continue;
            }
            if (state === State.Start) {
                if (blocks.length === 0) {
                    if (isWhitespace(ch) && formatted.length === 0) {
                        continue;
                    }
                }
                // Copy white spaces and control characters
                if (ch <= ' ' || ch.charCodeAt(0) >= 128) {
                    state = State.Start;
                    formatted += ch;
                    continue;
                }
                // Selector or at-rule
                if (isName(ch) || (ch === '@')) {
                    // Clear trailing whitespaces and linefeeds.
                    str = trimRight(formatted);
                    if (str.length === 0) {
                        // If we have empty string after removing all the trailing
                        // spaces, that means we are right after a block.
                        // Ensure a blank line as the separator.
                        if (blocks.length > 0) {
                            formatted = '\n\n';
                        }
                    } else {
                        // After finishing a ruleset or directive statement,
                        // there should be one blank line.
                        if (str.charAt(str.length - 1) === '}' ||
                            str.charAt(str.length - 1) === ';') {
                            formatted = str + '\n\n';
                        } else {
                            // After block comment, keep all the linefeeds but
                            // start from the first column (remove whitespaces prefix).
                            while (true) {
                                ch2 = formatted.charAt(formatted.length - 1);
                                if (ch2 !== ' ' && ch2.charCodeAt(0) !== 9) {
                                    break;
                                }
                                formatted = formatted.substr(0, formatted.length - 1);
                            }
                        }
                    }
                    formatted += ch;
                    state = (ch === '@') ? State.AtRule : State.Selector;
                    continue;
                }
            }
            if (state === State.AtRule) {
                // ';' terminates a statement.
                if (ch === ';') {
                    formatted += ch;
                    state = State.Start;
                    continue;
                }
                // '{' starts a block
                if (ch === '{') {
                    str = trimRight(formatted);
                    openBlock();
                    state = (str === '@font-face') ? State.Ruleset : State.Block;
                    continue;
                }
                formatted += ch;
                continue;
            }
            if (state === State.Block) {
                // Selector
                if (isName(ch)) {
                    // Clear trailing whitespaces and linefeeds.
                    str = trimRight(formatted);
                    if (str.length === 0) {
                        // If we have empty string after removing all the trailing
                        // spaces, that means we are right after a block.
                        // Ensure a blank line as the separator.
                        if (blocks.length > 0) {
                            formatted = '\n\n';
                        }
                    } else {
                        // Insert blank line if necessary.
                        if (str.charAt(str.length - 1) === '}') {
                            formatted = str + '\n\n';
                        } else {
                            // After block comment, keep all the linefeeds but
                            // start from the first column (remove whitespaces prefix).
                            while (true) {
                                ch2 = formatted.charAt(formatted.length - 1);
                                if (ch2 !== ' ' && ch2.charCodeAt(0) !== 9) {
                                    break;
                                }
                                formatted = formatted.substr(0, formatted.length - 1);
                            }
                        }
                    }
                    appendIndent();
                    formatted += ch;
                    state = State.Selector;
                    continue;
                }
                // '}' resets the state.
                if (ch === '}') {
                    closeBlock();
                    state = State.Start;
                    continue;
                }
                formatted += ch;
                continue;
            }
            if (state === State.Selector) {
                // '{' starts the ruleset.
                if (ch === '{') {
                    openBlock();
                    state = State.Ruleset;
                    continue;
                }
                // '}' resets the state.
                if (ch === '}') {
                    closeBlock();
                    state = State.Start;
                    continue;
                }
                formatted += ch;
                continue;
            }
            if (state === State.Ruleset) {
                // '}' finishes the ruleset.
                if (ch === '}') {
                    closeBlock();
                    state = State.Start;
                    if (depth > 0) {
                        state = State.Block;
                    }
                    continue;
                }
                // Make sure there is no blank line or trailing spaces inbetween
                if (ch === '\n') {
                    formatted = trimRight(formatted);
                    formatted += '\n';
                    continue;
                }
                // property name
                if (!isWhitespace(ch)) {
                    formatted = trimRight(formatted);
                    formatted += '\n';
                    appendIndent();
                    formatted += ch;
                    state = State.Property;
                    continue;
                }
                formatted += ch;
                continue;
            }
            if (state === State.Property) {
                // ':' concludes the property.
                if (ch === ':') {
                    formatted = trimRight(formatted);
                    formatted += ': ';
                    state = State.Expression;
                    if (isWhitespace(ch2)) {
                        state = State.Separator;
                    }
                    continue;
                }
                // '}' finishes the ruleset.
                if (ch === '}') {
                    closeBlock();
                    state = State.Start;
                    if (depth > 0) {
                        state = State.Block;
                    }
                    continue;
                }
                formatted += ch;
                continue;
            }
            if (state === State.Separator) {
                // Non-whitespace starts the expression.
                if (!isWhitespace(ch)) {
                    formatted += ch;
                    state = State.Expression;
                    continue;
                }
                // Anticipate string literal.
                if (isQuote(ch2)) {
                    state = State.Expression;
                }
                continue;
            }
            if (state === State.Expression) {
                // '}' finishes the ruleset.
                if (ch === '}') {
                    closeBlock();
                    state = State.Start;
                    if (depth > 0) {
                        state = State.Block;
                    }
                    continue;
                }
                // ';' completes the declaration.
                if (ch === ';') {
                    formatted = trimRight(formatted);
                    formatted += ';\n';
                    state = State.Ruleset;
                    continue;
                }
                formatted += ch;
                if (ch === '(') {
                    if (formatted.charAt(formatted.length - 2) === 'l' &&
                        formatted.charAt(formatted.length - 3) === 'r' &&
                        formatted.charAt(formatted.length - 4) === 'u') {
                        // URL starts with '(' and closes with ')'.
                        state = State.URL;
                        continue;
                    }
                }
                continue;
            }
            if (state === State.URL) {
                // ')' finishes the URL (only if it is not escaped).
                if (ch === ')' && formatted.charAt(formatted.length - 1 !== '\\')) {
                    formatted += ch;
                    state = State.Expression;
                    continue;
                }
            }
            // The default action is to copy the character (to prevent
            // infinite loop).
            formatted += ch;
        }
        formatted = blocks.join('') + formatted;
 
        if (firstChar) {
            formatted = formatted.split('\n').map(function (fm) {
                return firstChar + fm + '\n';
            });
            formatted = formatted.join('');
        }
 
        return formatted;
    }
    if (typeof exports !== 'undefined') {
        // Node.js module.
        module.exports.css_beautify = cssbeautify;
    } else if (typeof window === 'object') {
        // Browser loading.
        window.css_beautify = cssbeautify;
    }
}());

4、参考资料

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

关于作者

柒夜笙歌凉

暂无简介

0 文章
0 评论
357 人气
更多

推荐作者

6118422078

文章 0 评论 0

Bonjour°[大白

文章 0 评论 0

別甾虛僞

文章 0 评论 0

qq_FynBW0

文章 0 评论 0

浅笑依然

文章 0 评论 0

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文