Skip to content

Commit

Permalink
add option to separately indent js/css in html
Browse files Browse the repository at this point in the history
  • Loading branch information
nightwing committed Dec 23, 2011
1 parent 27e91ad commit 177dfa1
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 12 deletions.
24 changes: 16 additions & 8 deletions beautify-html.js
Expand Up @@ -23,6 +23,7 @@
brace_style (default "collapse") - "collapse" | "expand" | "end-expand"
put braces on the same line as control statements (default), or put braces on own line (Allman / ANSI style), or just put end braces on own line.
unformatted (default ['a']) - list of tags, that shouldn't be reformatted
indent_scripts (default normal) - "keep"|"separate"|"normal"
e.g.
Expand Down Expand Up @@ -360,15 +361,14 @@ function style_html(html_source, options) {
}

this.get_full_indent = function (level) {
if (level == null)
level = this.indent_level;
level = this.indent_level + level || 0;
if (level < 1)
return '';

return Array(level + 1).join(this.indent_string);
}


this.printer = function (js_source, indent_character, indent_size, max_char, brace_style) { //handles input/output and some other printing functions

this.input = js_source || ''; //gets the input for the Parser
Expand Down Expand Up @@ -475,16 +475,24 @@ function style_html(html_source, options) {
} else if (multi_parser.token_type == 'TK_STYLE') {
var _beautifier = typeof css_beautify == 'function' && css_beautify;
}

var indentation = multi_parser.get_full_indent();

if (options.indent_scripts == "keep") {
var script_indent_level = 0;
} else if (options.indent_scripts == "separate") {
var script_indent_level = -multi_parser.indent_level;
} else {
var script_indent_level = 1;
}

var indentation = multi_parser.get_full_indent(script_indent_level);
if (_beautifier) {
// call the Beautifier if avaliable
text = _beautifier(text.replace(/^\s*/, indentation), options);
} else {
// simply indent the string otherwise
var white = text.match(/^\s*/)[0];
var _level = white.match(/[^\n\r]*$/)[0].split(multi_parser.indent_string).length - 1;
var reindent = multi_parser.get_full_indent(multi_parser.indent_level - _level);
var reindent = multi_parser.get_full_indent(script_indent_level -_level);
text = text.replace(/^\s*/, indentation)
.replace(/\r\n|\r|\n/g, '\n' + reindent)
.replace(/\s*$/, '');
Expand Down
23 changes: 19 additions & 4 deletions index.html
Expand Up @@ -226,7 +226,7 @@
$('#detect-packers').attr('checked', $.cookie('detect-packers') !== 'off');
$('#preserve-newlines').attr('checked', $.cookie('preserve-newlines') !== 'off');
$('#keep-array-indentation').attr('checked', $.cookie('keep-array-indentation') === 'on');
}
$('#indent-scripts').val(any($.cookie('indent-scripts'), 'normal'));}

function store_settings_to_cookie() {
var opts = { expires: 360 };
Expand All @@ -235,7 +235,7 @@
$.cookie('detect-packers', $('#detect-packers').attr('checked') ? 'on' : 'off', opts);
$.cookie('preserve-newlines', $('#preserve-newlines').attr('checked') ? 'on' : 'off', opts);
$.cookie('keep-array-indentation', $('#keep-array-indentation').attr('checked') ? 'on' : 'off', opts);
}
$.cookie('indent-scripts', $('#indent-scripts').val(), opts);}

function unpacker_filter(source) {
var trailing_comments = '';
Expand Down Expand Up @@ -286,6 +286,7 @@
var indent_char = indent_size == 1 ? '\t' : ' ';
var preserve_newlines = $('#preserve-newlines').attr('checked');
var keep_array_indentation = $('#keep-array-indentation').attr('checked');
var indent_scripts = $('#indent-scripts').val();
var brace_style = $('#brace-style').val();

if ($('#detect-packers').attr('checked')) {
Expand All @@ -299,7 +300,8 @@
preserve_newlines:preserve_newlines,
brace_style: brace_style,
keep_array_indentation:keep_array_indentation,
space_after_anon_function:true};
space_after_anon_function:true,
indent_scripts:indent_scripts};

if (source && source[0] === '<' && source.substring(0, 4) !== comment_mark) {
$('#source').val(
Expand All @@ -322,7 +324,15 @@

read_settings_from_cookie();

var default_text = "// This is just a sample script. Paste your real code here.\nif ('this_is'==/an_example/){of_beautifer();}else{var a=b?(c%d):e[f];}";
var default_text = [
'\<head>\<style>',
'/*a css comment*/a#s .i[ sp ="true" ]{color: red;back:url(data:;");}',
'\<\/style ><script>',
"\n\n// This is just a sample script. Paste your real code here.\n\nif ('this_is'==/an_example/){of_beautifer();}else{var a=b?(c%d):e[f];}",
'\<\/script>\</head>',
'\<div>\<A href="as">'
].join("");

$('#source').val(default_text).bind('click focus', function () {
if ($(this).val() == default_text) {
$(this).val('');
Expand Down Expand Up @@ -358,6 +368,11 @@
<option value="expand">Braces on own line</option>
<option value="end-expand">End braces on own line</option>
</select></li>
<li><select id="indent-scripts">
<option value="keep">Keep indent level of the tag</option>
<option value="normal">Add one indent level</option>
<option value="separate">Separate indentaition</option>
</select></li>
<li><input class="checkbox" type="checkbox" id="preserve-newlines" /><label for="preserve-newlines"> Preserve empty lines?</label><br /></li>
<li><input class="checkbox" type="checkbox" id="detect-packers" /><label for="detect-packers"> Detect packers and obfuscators?</label><br /></li>
<li><input class="checkbox" type="checkbox" id="keep-array-indentation" /><label for="keep-array-indentation"> Keep array indentation?</label></li>
Expand Down

0 comments on commit 177dfa1

Please sign in to comment.