Coder Social home page Coder Social logo

htmlbeautifier's Introduction

HTML Beautifier

A normaliser/beautifier for HTML that also understands embedded Ruby. Ideal for tidying up Rails templates.

What it does

  • Normalises hard tabs to spaces (or vice versa)
  • Removes trailing spaces
  • Indents after opening HTML elements
  • Outdents before closing elements
  • Collapses multiple whitespace
  • Indents after block-opening embedded Ruby (if, do etc.)
  • Outdents before closing Ruby blocks
  • Outdents elsif and then indents again
  • Indents the left-hand margin of JavaScript and CSS blocks to match the indentation level of the code

Usage

From the command line

To update files in-place:

$ htmlbeautifier file1.html.erb [file2.html.erb ...]

or to operate on standard input and output:

$ htmlbeautifier < untidy.html.erb > formatted.html.erb

In your code

require 'htmlbeautifier'

beautiful = HtmlBeautifier.beautify(untify_html_string)

You can also specify how to indent (the default is two spaces):

beautiful = HtmlBeautifier.beautify(untidy_html_string, indent: "\t")

Installation

This is a Ruby gem. To install the command-line tool (you may need sudo):

$ gem install htmlbeautifier

To use the gem with Bundler, add to your Gemfile:

gem 'htmlbeautifier'

Contributing

  1. Follow these guidelines when writing commit messages (briefly, the first line should begin with a capital letter, use the imperative mood, be no more than 50 characters, and not end with a period).
  2. Include tests.

htmlbeautifier's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

htmlbeautifier's Issues

Is there a way to specify file encoding?

Hi there:

I'm using htmlbeautifier and it works great (good job πŸ‘ ), but I would like to know if there's a way to specify file encoding for .erb files because htmlbeautifier complains about:

invalid byte sequence in US-ASCII on line 1

This is because I use unicode glyphs as icons and the beautifier fails to understand them.

Thanks in advance,
Ivan Zaera

Retain intentional line breaks

It would be great if there were an option to retain line breaks that the developer places in the file intentionally to make readability of the partial easier. For instance:

<% page_title! "Project: #{project.name}" %>

<main>
  <header>
...

which currently gets turned into

<% page_title! "Project: #{project.name}" %>
<main>
  <header>
...

Support for erb formatting in vscode?

How can I use htmlbeautifier for erb formatting in vscode?
I use rufo for rb file formatting in vscode, but haven't got any solutions for erb formatting.
Anyone can help out?

Allow generic self-closing blocks

Right not it looks like all the possible elements that can be self-closed without adding an indentation level are hard-coded. Which means if you have some sort of custom element (e.g. later replaced by vue) that is self-closed, it will break the indentation formatting

Works indefinitely when processing file

$ htmlbeautifier app/views/project/_summary.html.erb
indefinitely long
_summary.html.erb.txt

Please rename _summary.html.erb.txt to _summary.html.erb

$ gem which htmlbeautifier
/home/srghma/.gem/ruby/2.4.0/gems/htmlbeautifier-1.3.1/lib/htmlbeautifier.rb

Then I click ctrl-c

$ htmlbeautifier app/views/project/_summary.html.erb
^C/home/srghma/.gem/ruby/2.4.0/gems/htmlbeautifier-1.3.1/lib/htmlbeautifier/parser.rb:30:in `scan': Interrupt
        from /home/srghma/.gem/ruby/2.4.0/gems/htmlbeautifier-1.3.1/lib/htmlbeautifier/parser.rb:30:in `block in dispatch'
        from /home/srghma/.gem/ruby/2.4.0/gems/htmlbeautifier-1.3.1/lib/htmlbeautifier/parser.rb:30:in `each'
        from /home/srghma/.gem/ruby/2.4.0/gems/htmlbeautifier-1.3.1/lib/htmlbeautifier/parser.rb:30:in `find'
        from /home/srghma/.gem/ruby/2.4.0/gems/htmlbeautifier-1.3.1/lib/htmlbeautifier/parser.rb:30:in `dispatch'
        from /home/srghma/.gem/ruby/2.4.0/gems/htmlbeautifier-1.3.1/lib/htmlbeautifier/parser.rb:16:in `scan'
        from /home/srghma/.gem/ruby/2.4.0/gems/htmlbeautifier-1.3.1/lib/htmlbeautifier.rb:26:in `block in beautify'
        from /home/srghma/.gem/ruby/2.4.0/gems/htmlbeautifier-1.3.1/lib/htmlbeautifier.rb:25:in `tap'
        from /home/srghma/.gem/ruby/2.4.0/gems/htmlbeautifier-1.3.1/lib/htmlbeautifier.rb:25:in `beautify'
        from /home/srghma/.gem/ruby/2.4.0/gems/htmlbeautifier-1.3.1/bin/htmlbeautifier:7:in `beautify'
        from /home/srghma/.gem/ruby/2.4.0/gems/htmlbeautifier-1.3.1/bin/htmlbeautifier:74:in `block (2 levels) in <top (required)>'
        from /home/srghma/.gem/ruby/2.4.0/gems/htmlbeautifier-1.3.1/bin/htmlbeautifier:73:in `open'
        from /home/srghma/.gem/ruby/2.4.0/gems/htmlbeautifier-1.3.1/bin/htmlbeautifier:73:in `block in <top (required)>'
        from /home/srghma/.gem/ruby/2.4.0/gems/htmlbeautifier-1.3.1/bin/htmlbeautifier:70:in `each'
        from /home/srghma/.gem/ruby/2.4.0/gems/htmlbeautifier-1.3.1/bin/htmlbeautifier:70:in `<top (required)>'
        from /home/srghma/.gem/ruby/2.4.0/bin/htmlbeautifier:23:in `load'
        from /home/srghma/.gem/ruby/2.4.0/bin/htmlbeautifier:23:in `<main>'

`uninitialized constant Pathname` when running with `--lint-only`

I think this is just missing a require "pathname":

$ htmlbeautifier **/*.html.erb --lint-only
Traceback (most recent call last):
        4: from /Users/andyw8/.gem/ruby/2.7.5/bin/htmlbeautifier:23:in `<main>'
        3: from /Users/andyw8/.gem/ruby/2.7.5/bin/htmlbeautifier:23:in `load'
        2: from /Users/andyw8/.gem/ruby/2.7.5/gems/htmlbeautifier-1.4.1/bin/htmlbeautifier:104:in `<top (required)>'
        1: from /Users/andyw8/.gem/ruby/2.7.5/gems/htmlbeautifier-1.4.1/bin/htmlbeautifier:104:in `map'
/Users/andyw8/.gem/ruby/2.7.5/gems/htmlbeautifier-1.4.1/bin/htmlbeautifier:105:in `block in <top (required)>': uninitialized constant Pathname (NameError)
$ ruby -v
ruby 2.7.5p203 (2021-11-24 revision f69aeb8314) [arm64-darwin21]

`>` in attribute breaks formatting

<div foo="a>b"
     bar="1">

is incorrectly formatted to

<div foo="a>b"
  bar="1">

(Without the > it works correctly).

This causes problems for Hotwire/Stimulus since it uses notation such as:

<button data-action="click->hello#greet">Greet</button>

Strange behavior with figure/code/pre blocks (liquid)

I use this plugin to render the HTML generated with Jekyll.
When I have a code snippet and I normalize it with htmlbeautifier, it ends up having a bad format (visually). The code snippet in liquid is:

{% highlight c++ linenos %}

int main(int argc, char const \*argv[])
{
    string myString;

    cout << "input a string: ";
    getline(cin, myString);
    int length = myString.length();

    char charArray = new char * [length];

    charArray = myString;
    for(int i = 0; i < length; ++i){
        cout << charArray[i] << " ";
    }

    return 0;
}

{% endhighlight %}

When I don't use the plugin the output is:

<figure class="highlight"><pre><code class="language-c--" data-lang="c++"><table class="rouge-table"><tbody><tr>
<td class="gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
</pre></td>
<td class="code"><pre><span class="kt">int</span> <span class="nf">main</span><span class="p">(</span><span class="kt">int</span> <span class="n">argc</span><span class="p">,</span> <span class="kt">char</span> <span class="k">const</span> <span class="err">\</span><span class="o">*</span><span class="n">argv</span><span class="p">[])</span>
<span class="p">{</span>
    <span class="n">string</span> <span class="n">myString</span><span class="p">;</span>

    <span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">"input a string: "</span><span class="p">;</span>
    <span class="n">getline</span><span class="p">(</span><span class="n">cin</span><span class="p">,</span> <span class="n">myString</span><span class="p">);</span>
    <span class="kt">int</span> <span class="n">length</span> <span class="o">=</span> <span class="n">myString</span><span class="p">.</span><span class="n">length</span><span class="p">();</span>

    <span class="kt">char</span> <span class="n">charArray</span> <span class="o">=</span> <span class="k">new</span> <span class="kt">char</span> <span class="o">*</span> <span class="p">[</span><span class="n">length</span><span class="p">];</span>

    <span class="n">charArray</span> <span class="o">=</span> <span class="n">myString</span><span class="p">;</span>
    <span class="k">for</span><span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">length</span><span class="p">;</span> <span class="o">++</span><span class="n">i</span><span class="p">){</span>
        <span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="n">charArray</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">&lt;&lt;</span> <span class="s">" "</span><span class="p">;</span>
    <span class="p">}</span>

    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>
</pre></td>
</tr></tbody></table></code></pre></figure>

Visual preview (without htmlbeautifier):

Screenshot of Google Chrome (10-12-21, 00-55-08)

With htmlbeautifier the output is:

<figure class="highlight">
          <pre><code class="language-c--" data-lang="c++"><table class="rouge-table">
<tbody>
<tr>
<td class="gutter gl">
<pre class="lineno">1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
</pre>
        </td>
        <td class="code">
          <pre><span class="kt">int</span> <span class="nf">main</span><span class="p">(</span><span class="kt">int</span> <span class="n">argc</span><span class="p">,</span> <span class="kt">char</span> <span class="k">const</span> <span class="err">\</span><span class="o">*</span><span class="n">argv</span><span class="p">[])</span>
<span class="p">{</span>
    <span class="n">string</span> <span class="n">myString</span><span class="p">;</span>

    <span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">"input a string: "</span><span class="p">;</span>
    <span class="n">getline</span><span class="p">(</span><span class="n">cin</span><span class="p">,</span> <span class="n">myString</span><span class="p">);</span>
    <span class="kt">int</span> <span class="n">length</span> <span class="o">=</span> <span class="n">myString</span><span class="p">.</span><span class="n">length</span><span class="p">();</span>

    <span class="kt">char</span> <span class="n">charArray</span> <span class="o">=</span> <span class="k">new</span> <span class="kt">char</span> <span class="o">*</span> <span class="p">[</span><span class="n">length</span><span class="p">];</span>

    <span class="n">charArray</span> <span class="o">=</span> <span class="n">myString</span><span class="p">;</span>
    <span class="k">for</span><span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">length</span><span class="p">;</span> <span class="o">++</span><span class="n">i</span><span class="p">){</span>
        <span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="n">charArray</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">&lt;&lt;</span> <span class="s">" "</span><span class="p">;</span>
    <span class="p">}</span>

    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>
</pre>
        </td>
      </tr>
    </tbody>
  </table>
</code></pre>
</figure>

Visual preview (with htmlbeautifier):

Screenshot of Google Chrome (10-12-21, 00-57-45)

I still can't find where the problem is generated. Any idea?
I had thought of filtering the figure block so that it is not processed, but I don't see how to do it.
Thanks

case without matcher

It's not a mayor issue, but the class: "something" notation it's not supported, it is perceived as a class definition instead of a symbol definition so mess the indentation

f.inputs Phone.model_name.human.pluralize do
            f.has_many :phones, allow_destroy: true, heading: nil do |phone|
                phone.input :area_code_id, as: :select, collection: AreaCode.all.order(:area_code).pluck(:area_code, :id), wrapper_html: { class: "phone_area_code_wrapper" }
                                                                                                                                             phone.input :phone_number, wrapper_html: { class: "phone_phone_number_wrapper" }
                                                                                                                                                                                        end
                                                                                                                                                                                        end
                                                                                                                                                                                        f.actions
                                                                                                                                                                                        end

like i say, it's not a mayor issue, the :class => "" notation works fine and is rare to have this kind of definitions in .rb files anyway(active_admin), regards!

Formatting javascript_tag

I'm working in an app that has a handful of inline script tags. During the process of configuring our content security policy to apply a nonce to these inline scrips, I replaced the raw <script> with a javascript_tag(nonce: true) do ... end.

This seems to confuse htmlbeautfier. It's removing all leading whitespace from the contents of the javascript_tag block.

Ideally it would just ignore this content --- or perhaps even offer an optional html comment to enable/disable the beautifier for a certain code blocks?

Thank you!

Different results in Bash or Zsh

When I run htmlbeautifier I get different results depending on whether I run it from Bash or Zsh.

Command

bundle exec htmlbeautifier build/**/*.html

Input

<div><div></div></div>
<div>
<div>
</div>
</div>

Bash Output

<div><div></div></div>
<div>
  <div>
  </div>
</div>

Zsh Output

<div>
  <div>
  </div>
</div>
<div>
  <div>
  </div>
</div>

How to require?

How do you require this. I installed this but on require 'htmlbeautifier' there is just a load error
LoadError: no such file to load -- htmlbeautifier

Failure `different prefix` when using a relative path

Thanks for the very useful library. I ran into this issue:

% bundle exec htmlbeautifier app/foo/bar/index.html.erb --lint-only
bundler: failed to load command: htmlbeautifier (/Users/andyw8/.gem/ruby/2.7.5/bin/htmlbeautifier)
Traceback (most recent call last):
	20: from /Users/andyw8/src/github.com/my-org/my-repo/bin/bundle:113:in `<main>'
	19: from /Users/andyw8/src/github.com/my-org/my-repo/bin/bundle:113:in `load'
	18: from /Users/andyw8/.gem/ruby/2.7.5/gems/bundler-2.3.7/exe/bundle:36:in `<top (required)>'
	17: from /Users/andyw8/.gem/ruby/2.7.5/gems/bundler-2.3.7/lib/bundler/friendly_errors.rb:103:in `with_friendly_errors'
	16: from /Users/andyw8/.gem/ruby/2.7.5/gems/bundler-2.3.7/exe/bundle:48:in `block in <top (required)>'
	15: from /Users/andyw8/.gem/ruby/2.7.5/gems/bundler-2.3.7/lib/bundler/cli.rb:25:in `start'
	14: from /Users/andyw8/.gem/ruby/2.7.5/gems/bundler-2.3.7/lib/bundler/vendor/thor/lib/thor/base.rb:485:in `start'
	13: from /Users/andyw8/.gem/ruby/2.7.5/gems/bundler-2.3.7/lib/bundler/cli.rb:31:in `dispatch'
	12: from /Users/andyw8/.gem/ruby/2.7.5/gems/bundler-2.3.7/lib/bundler/vendor/thor/lib/thor.rb:392:in `dispatch'
	11: from /Users/andyw8/.gem/ruby/2.7.5/gems/bundler-2.3.7/lib/bundler/vendor/thor/lib/thor/invocation.rb:127:in `invoke_command'
	10: from /Users/andyw8/.gem/ruby/2.7.5/gems/bundler-2.3.7/lib/bundler/vendor/thor/lib/thor/command.rb:27:in `run'
	 9: from /Users/andyw8/.gem/ruby/2.7.5/gems/bundler-2.3.7/lib/bundler/cli.rb:484:in `exec'
	 8: from /Users/andyw8/.gem/ruby/2.7.5/gems/bundler-2.3.7/lib/bundler/cli/exec.rb:23:in `run'
	 7: from /Users/andyw8/.gem/ruby/2.7.5/gems/bundler-2.3.7/lib/bundler/cli/exec.rb:58:in `kernel_load'
	 6: from /Users/andyw8/.gem/ruby/2.7.5/gems/bundler-2.3.7/lib/bundler/cli/exec.rb:58:in `load'
	 5: from /Users/andyw8/.gem/ruby/2.7.5/bin/htmlbeautifier:23:in `<top (required)>'
	 4: from /Users/andyw8/.gem/ruby/2.7.5/bin/htmlbeautifier:23:in `load'
	 3: from /Users/andyw8/.gem/ruby/2.7.5/gems/htmlbeautifier-1.4.1/bin/htmlbeautifier:104:in `<top (required)>'
	 2: from /Users/andyw8/.gem/ruby/2.7.5/gems/htmlbeautifier-1.4.1/bin/htmlbeautifier:104:in `map'
	 1: from /Users/andyw8/.gem/ruby/2.7.5/gems/htmlbeautifier-1.4.1/bin/htmlbeautifier:106:in `block in <top (required)>'
/opt/rubies/2.7.5/lib/ruby/2.7.0/pathname.rb:522:in `relative_path_from': different prefix: "" and "/Users/andyw8/src/github.com/my-org/my-repo" (ArgumentError)

If I instead provide an absolute path, it works.

The error seems to be from within Ruby itself, but I'm not entirely sure what's happening.

Mac OS 12.2.1, Ruby 2.7

I can provide further information if you're unable to reproduce.

Outdented too far

Hello, thank you for the gem.

A minor issue in my usage, the "Outdented too far" exception. In my case, I don't really have anything else to do if it's incorrectly outdented. So I would just ignore it.

#      raise "Outdented too far" if @level < 0
      @level = 0 if @level < 0

Command line tool is slow

With the current command line tool, processing erb files in the following way is slow:

$ htmlbeautifier file1 [file2 ...]

Something like this is 10x faster for my project:

Dir.glob("app/views/**/*.erb").each do |erb_file|
  input = File.read(erb_file)
  output = HtmlBeautifier.beautify(input, keep_blank_lines: 1) + "\n"
  unless input == output
    File.write(erb_file, output)
  end
end

Maybe this can be added to the project?

Hangs forever on id="<%= some %>"

I have an html.erb file.
htmlbeautifier is version (1.3.1)

I have a large erb file.
I execute

htmlbeautifier the_file.html.erb

This command hangs forever and does not return.

In the file I have

<article id='<%= @holiday_content.to_param %>'>
              </article>

If I remove the id and leave it to just

<article id='ddd'>
              </article>

It works again.

Stuck forever when formatting erb file

Hello,

I install ERB Formatter/Beautify plugin in VS Code and format the attachment file, but it stuck forever with heavy CPU and overheat.

If I running the command from terminal:

htmlbeautifier app/views/...file_name.html.eb

It stuck forever in terminal command.

kevin@local:~/developer/$ htmlbeautifier app/views/common/file_name.html.erb
^CTraceback (most recent call last):
	18: from /home/kevin/.rvm/gems/ruby-2.7.2/bin/ruby_executable_hooks:22:in `<main>'
	17: from /home/kevin/.rvm/gems/ruby-2.7.2/bin/ruby_executable_hooks:22:in `eval'
	16: from /home/kevin/.rvm/gems/ruby-2.7.2/bin/htmlbeautifier:23:in `<main>'
	15: from /home/kevin/.rvm/gems/ruby-2.7.2/bin/htmlbeautifier:23:in `load'
	14: from /home/kevin/.rvm/gems/ruby-2.7.2/gems/htmlbeautifier-1.3.1/bin/htmlbeautifier:70:in `<top (required)>'
	13: from /home/kevin/.rvm/gems/ruby-2.7.2/gems/htmlbeautifier-1.3.1/bin/htmlbeautifier:70:in `each'
	12: from /home/kevin/.rvm/gems/ruby-2.7.2/gems/htmlbeautifier-1.3.1/bin/htmlbeautifier:73:in `block in <top (required)>'
	11: from /home/kevin/.rvm/gems/ruby-2.7.2/gems/htmlbeautifier-1.3.1/bin/htmlbeautifier:73:in `open'
	10: from /home/kevin/.rvm/gems/ruby-2.7.2/gems/htmlbeautifier-1.3.1/bin/htmlbeautifier:74:in `block (2 levels) in <top (required)>'
	 9: from /home/kevin/.rvm/gems/ruby-2.7.2/gems/htmlbeautifier-1.3.1/bin/htmlbeautifier:7:in `beautify'
	 8: from /home/kevin/.rvm/gems/ruby-2.7.2/gems/htmlbeautifier-1.3.1/lib/htmlbeautifier.rb:25:in `beautify'
	 7: from /home/kevin/.rvm/gems/ruby-2.7.2/gems/htmlbeautifier-1.3.1/lib/htmlbeautifier.rb:25:in `tap'
	 6: from /home/kevin/.rvm/gems/ruby-2.7.2/gems/htmlbeautifier-1.3.1/lib/htmlbeautifier.rb:26:in `block in beautify'
	 5: from /home/kevin/.rvm/gems/ruby-2.7.2/gems/htmlbeautifier-1.3.1/lib/htmlbeautifier/parser.rb:16:in `scan'
	 4: from /home/kevin/.rvm/gems/ruby-2.7.2/gems/htmlbeautifier-1.3.1/lib/htmlbeautifier/parser.rb:30:in `dispatch'
	 3: from /home/kevin/.rvm/gems/ruby-2.7.2/gems/htmlbeautifier-1.3.1/lib/htmlbeautifier/parser.rb:30:in `find'
	 2: from /home/kevin/.rvm/gems/ruby-2.7.2/gems/htmlbeautifier-1.3.1/lib/htmlbeautifier/parser.rb:30:in `each'
	 1: from /home/kevin/.rvm/gems/ruby-2.7.2/gems/htmlbeautifier-1.3.1/lib/htmlbeautifier/parser.rb:30:in `block in dispatch'
/home/kevin/.rvm/gems/ruby-2.7.2/gems/htmlbeautifier-1.3.1/lib/htmlbeautifier/parser.rb:30:in `scan': Interrupt

Please use the attached file for a test.

show.html.zip

Multi-line ERB tags get wrong formatting when the tags are misaligned

Hi there,

On our project, we'll often break our ERB tags into multiple lines to avoid long lines, and whenever an erb tag is not indented properly in the parent HTML tag, when htmlbeautifier is triggered, it will fix the opening of the tag but leave the following lines untouched, creating a misaligned tag.

Is this the expected behavior? Here's a GIF showing the scenario:

CleanShot 2022-05-19 at 10 37 48

Thank you.

requiring uninitialized errors

I'm assuming the require code needs to go at the top of application.rb?

application.rb
require 'rails/all'
require 'htmlbeautifier'
beautiful = HtmlBeautifier.beautify(messy).

When starting the server I experience errors:

  1. when using "beautiful = HtmlBeautifier.beautify(messy)" I get "method_missing': undefined local variable or methodmessy' for..."
  2. when using 'HtmlBeautifier::Beautifier.new($stdout).scan("\n\n\n")' I get "uninitialized constant HtmlBeautifier::Beautifier".

Is my config wrong or is there a bug? Thanks!

Undefined local variable

Hello,

Hope you're well. I'm seeing an error when running the gem. It was working fine but has now started producing this:

/Users/paul/.rvm/gems/ruby-1.9.3-p392/bundler/gems/htmlbeautifier-2be0d969f30e/lib/htmlbeautifier/parser.rb:58:in `rescue in dispatch': undefined local variable or method `match' for #<HtmlBeautifier::HtmlParser:0x007f8124d84260> on line 369 (RuntimeError)
    from /Users/paul/.rvm/gems/ruby-1.9.3-p392/bundler/gems/htmlbeautifier-2be0d969f30e/lib/htmlbeautifier/parser.rb:42:in `dispatch'
    from /Users/paul/.rvm/gems/ruby-1.9.3-p392/bundler/gems/htmlbeautifier-2be0d969f30e/lib/htmlbeautifier/parser.rb:28:in `scan'
    from /Users/paul/.rvm/gems/ruby-1.9.3-p392/bundler/gems/htmlbeautifier-2be0d969f30e/lib/htmlbeautifier/beautifier.rb:20:in `scan'
    from /Users/paul/.rvm/gems/ruby-1.9.3-p392/bundler/gems/htmlbeautifier-2be0d969f30e/bin/htmlbeautifier:5:in `beautify'
    from /Users/paul/.rvm/gems/ruby-1.9.3-p392/bundler/gems/htmlbeautifier-2be0d969f30e/bin/htmlbeautifier:13:in `block (2 levels) in <top (required)>'
    from /Users/paul/.rvm/gems/ruby-1.9.3-p392/bundler/gems/htmlbeautifier-2be0d969f30e/bin/htmlbeautifier:12:in `open'
    from /Users/paul/.rvm/gems/ruby-1.9.3-p392/bundler/gems/htmlbeautifier-2be0d969f30e/bin/htmlbeautifier:12:in `block in <top (required)>'
    from /Users/paul/.rvm/gems/ruby-1.9.3-p392/bundler/gems/htmlbeautifier-2be0d969f30e/bin/htmlbeautifier:10:in `each'
    from /Users/paul/.rvm/gems/ruby-1.9.3-p392/bundler/gems/htmlbeautifier-2be0d969f30e/bin/htmlbeautifier:10:in `<top (required)>'
    from /Users/paul/.rvm/gems/ruby-1.9.3-p392/bin/htmlbeautifier:19:in `load'
    from /Users/paul/.rvm/gems/ruby-1.9.3-p392/bin/htmlbeautifier:19:in `<main>'
    from /Users/paul/.rvm/gems/ruby-1.9.3-p392/bin/ruby_noexec_wrapper:14:in `eval'
    from /Users/paul/.rvm/gems/ruby-1.9.3-p392/bin/ruby_noexec_wrapper:14:in `<main>'

I'm using this in my gem file:

gem "htmlbeautifier", git: "https://github.com/threedaymonk/htmlbeautifier.git", branch: "ie-cc"

I wonder if some of my mark-up is causing it but have no idea how to track it down. Any help would be massively appreciated. :)

Escaping characters?

Is there a method to escape certain characters or hide a string from HTML Beautifier?

I am using the Fancybox plugin, and this bit of HTML returns undefined local variable or method 'match' in HTML Beautifier:

<button href="/" data-fancybox-type="iframe" title="My Link Copy :: <a href='http://www.website.com'>Launch website</a>">

It's the < and the > causing the problem. If I change them to HTML character entities, the link is not generated in Fancybox.

lowercase element names (tags)

My experience with other HTML tidiers leads me to expect that htmlbeautifier would support tag case normalization e.g. normalizing (upper, lower, etc.) the letter case of all element and attribute names, leaving element values untouched.

<A HREF="foo">bar</A> β†’ <a href="foo">bar</a>

That would be pretty handy to have in htmlbeautifier. Is there any objection to adding that? Would you be open to PRs?

Great!

This is a fantastic beautifier that actually works! Thanks.

Gets stuck on bigger files

When I try to beautify large files (specially html.erb files), It gets stuck and I have to forcefully exit the program. Why is that?

Where to put the "In your code" code?

The REAME simply states "In your code" for installing the gem. I have tried putting the suggested code in 3 different locations, with no success in installing this gem. Where should I put "require 'htmlbeautifier'" and where should I put "beautiful = HtmlBeautifier.beautify(messy)"? Is "messy" a variable I am supposed to set? Thanks!

Lint mode?

We want to lint our .html.erb files for valid HTML as a CI check. Can this libary do it?

Gives a `No such file or directory` and `block in <top (required)>` errors when executed

When trying to beautify my files like this htmlbeautifier rb.html.erb rb2.html.erb, it gives the following error :

    /home/pubudu/.rvm/gems/ruby-2.2.0/gems/htmlbeautifier-1.0.0/bin/htmlbeautifier:30:in `read': No such file or directory @ rb_sysopen - rb2.html.erb (Errno::ENOENT)
        from /home/pubudu/.rvm/gems/ruby-2.2.0/gems/htmlbeautifier-1.0.0/bin/htmlbeautifier:30:in `block in <top (required)>'
        from /home/pubudu/.rvm/gems/ruby-2.2.0/gems/htmlbeautifier-1.0.0/bin/htmlbeautifier:29:in `each'
        from /home/pubudu/.rvm/gems/ruby-2.2.0/gems/htmlbeautifier-1.0.0/bin/htmlbeautifier:29:in `<top (required)>'
        from /home/pubudu/.rvm/gems/ruby-2.2.0/bin/htmlbeautifier:23:in `load'
        from /home/pubudu/.rvm/gems/ruby-2.2.0/bin/htmlbeautifier:23:in `<main>'
        from /home/pubudu/.rvm/gems/ruby-2.2.0/bin/ruby_executable_hooks:15:in `eval'
        from /home/pubudu/.rvm/gems/ruby-2.2.0/bin/ruby_executable_hooks:15:in `<main>'

`Unmatched sequence` error for self-closing tags with newlines

given this html:

<img
  src="foo.jpg"
/>

... htmlbeautifier will throw an Unmatched sequence error

In console, this can be demonstrated easily with

# FAILS
HtmlBeautifier.beautify("<img\n src='foo.jpg'\n/>")

# this works fine
HtmlBeautifier.beautify("<img\n src='foo.jpg'\n/>".gsub("\n",""))

Could you make an option to let one empty row when there are multiple empty rows?

Sometimes its really easier to read html code when there are some parts seperated with empty lines.. At the moment all empty lines get removed, but i would like to have at least one sometimes..

So it would be nice if there is an option which gives you the power to config like "reduce empty lines to 1"..

Do you understand what i mean?

Conditional Comments - Indentation incorrect

Hi,

Love this gem. Really simple to use. I'm bolting on to Middleman, at the end of its build I run this on all the HTML Files. Works a treat apart from when it hits IE-Conditional comments.

This:

<!DOCTYPE html>
<!--[if lt IE 7]><html lang="en-us" class="ie6"><![endif]-->
<!--[if IE 7]><html lang="en-us" class="ie7"><![endif]-->
<!--[if IE 8]><html lang="en-us" class="ie8"><![endif]-->
<!--[if gt IE 8]><!--><html lang="en-us"><!--<![endif]-->

becomes:

<!DOCTYPE html>
<!--[if lt IE 7]>
    <html lang="en-us" class="ie6">
    <![endif]-->
    <!--[if IE 7]>
        <html lang="en-us" class="ie7">
        <![endif]-->
        <!--[if IE 8]>
            <html lang="en-us" class="ie8">
            <![endif]-->
            <!--[if gt IE 8]>
                <!-->
                <html lang="en-us">
                <!--<![endif]-->

Anyway to have it ignore/skip conditional comments? I'm not a ruby dev otherwise I'd have tried patching myself.

Problem of beautifier handlebars inside html

As you can see when I use handlebars inside html htmlbeautifier position correctly only html tags, but not handlebars. All handlebars tags are inline. What you can suggest?

  <script type="text/x-handlebars" data-template-name="lists">
    <section id="lists-container" class="lists-container">
      <div class="row">
        {{#each controller itemController="list"}}
        <section class="list list-inline">
          <h1>{{listName}}</h1> {{#each card in cards itemController="card"}}
          <div class="card">
            {{#if isEditing}} {{ view App.EditCardView }} {{else}}
            <label>{{card.description}}</label>
            <div class="edit-small edit-card hand" {{action "editCard" on="click" }}>Edit</div>
            <div {{action "deleteCard" card}} class="remove-small remove-card hand">X</div>
            {{/if}}
          </div>
          {{/each}}
          <a {{action "removeList"}} class="hand">
            <div class="remove remove-list">X</div>
          </a>
          <section class="new-card">
            {{input type="text" value=cardDescription action="createCard" class="card-input" placeholder="New Card"}}
          </section>
        </section>
        {{/each}}
        <section class="list new-list list-inline">
          {{input type="text" value=listName action="createList" id="new-list" class="list-input" placeholder="New List"}}
        </section>
      </div>
    </section>
  </script>

textarea needs to be treated as a preformatted element

I've noticed some odd behavior with <textarea>. Formatted indention is showing-up in field content. I believe it needs to be treated as a preformatted element.

Here is a test along with example fix based on the existing solution for <pre>.

diff --git a/lib/htmlbeautifier/html_parser.rb b/lib/htmlbeautifier/html_parser.rb
index 1d4285d..e18f843 100644
--- a/lib/htmlbeautifier/html_parser.rb
+++ b/lib/htmlbeautifier/html_parser.rb
@@ -31,6 +31,8 @@ module HtmlBeautifier
        :foreign_block],
       [%r{(<pre#{ELEMENT_CONTENT}>)(.*?)(</pre>)}omi,
        :preformatted_block],
+      [%r{(<textarea#{ELEMENT_CONTENT}>)(.*?)(</textarea>)}omi,
+       :preformatted_block],
       [%r{<#{HTML_VOID_ELEMENTS}(?: #{ELEMENT_CONTENT})?/?>}om,
        :standalone_element],
       [%r{</#{HTML_BLOCK_ELEMENTS}>}om,
diff --git a/spec/behavior_spec.rb b/spec/behavior_spec.rb
index 53c13cd..7609d6e 100644
--- a/spec/behavior_spec.rb
+++ b/spec/behavior_spec.rb
@@ -338,6 +338,21 @@ describe HtmlBeautifier do
     expect(described_class.beautify(source)).to eq(source)
   end

+  it "does not modify content of <textarea>" do
+    source = code <<-END
+      <div>
+        <textarea>   Preformatted   text
+
+                should  <em>not  be </em>
+                      modified,
+                ever!
+
+        </textarea>
+      </div>
+    END
+    expect(described_class.beautify(source)).to eq(source)
+  end
+

[ST2, Windows 7,x 64] Error: Invalid output. Check your interpreter settings

I installed the package in SublimeText2 on Windws 7, 64 bits.
I also installed the htmlbeautifier gem (https://github.com/threedaymonk/htmlbeautifier), then modified User settings in ST2 as follows as it was suggested in your README file:

{
    // Would you prefer a tab or two spaces to represent a tab
    // The default is two spaces represented by 'space'
    // anything else will use one tab character
    "file_patterns": ["\\.html\\.erb", "\\.rb", "\\.rake", "Rakefile", "Gemfile"],
    "html_erb_patterns": ["\\.html\\.erb"],
    "run_on_save": false,
    "save_on_beautify": true,
    "ruby": "ruby"
}

When hitting CTRL-ALT-k I got the below error displayed in a popup:

Error: Invalid output. Check your interpreter settings

I have Ruby installed:

C:\Windows\system32>ruby -v
ruby 2.1.5p273 (2014-11-13 revision 48405) [x64-mingw32]

So the formatting does not work as needed. Any idea ? Thank you

Not line breaking for form elements

html = '<form><label for="foo">Foo</label><input type="foo"/><label for="bar">Bar</label><input type="bar"/></form>'
HtmlBeautifier.beautify(html)

=> "<form><label for=\"foo\">Foo</label><input type=\"foo\"/><label for=\"bar\">Bar</label><input type=\"bar\"/></form>"

As you can see, everything is still dumped into a single line on the output

why it broke the indentation when I put erb inside div attributes?

what I get

  <div data-controller="autocomplete influencer-field" data-autocomplete-url-value=<%= search_influencers_path %> style="display: inline-block">
    <%= form.label :name, "name" %>
    <input type="text" data-action="influencer-field#syncInput" data-influencer-field-target="input" data-autocomplete-target="input" value='<%= params[:name] %>'/>
  <%= form.hidden_field :name, value: params[:name], data: {"autocomplete-target": "hidden", "influencer-field-target": "hidden"} %>
  <ul data-autocomplete-target="results"></ul>
</div>

what I expected

  <div data-controller="autocomplete influencer-field" data-autocomplete-url-value=<%= search_influencers_path %> style="display: inline-block">
    <%= form.label :name, "name" %>
    <input type="text" data-action="influencer-field#syncInput" data-influencer-field-target="input" data-autocomplete-target="input" value='<%= params[:name] %>'/>
    <%= form.hidden_field :name, value: params[:name], data: {"autocomplete-target": "hidden", "influencer-field-target": "hidden"} %>
    <ul data-autocomplete-target="results"></ul>
  </div>

I wondering in somefiles the < broke the indentation but somefiles not

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.