slim icon indicating copy to clipboard operation
slim copied to clipboard

Is there a code formatter for slim?

Open danlo opened this issue 11 years ago • 6 comments

Greetings,

There have been several methods people have been using to produce slim at my site.

 h1 Text

 h1
     | Text

Is there a "code formatter" for slim? that will clean up the code formatting?

-daniel

danlo avatar Dec 16 '14 19:12 danlo

+1

rskelley9 avatar Dec 30 '14 20:12 rskelley9

You could use another gem to do some text processing, like html2slim (which ships an erb2slim executable):

$ cat input.slim
h1 Text

h1
    | Text
$ slimrb --erb input.slim intermediate.erb
$ cat intermediate.erb
<h1>Text
</h1>
<h1>
Text
</h1>
$ erb2slim intermediate.erb normalized.slim
h1
  | Text 
h1
  |  Text

The extra whitespace is propably related to #440, though this is just a lucky guess.

There might be a way to perform this in pure Ruby (i.e. without invoking sub-processes or creating intermediate files) with the html2slim gem, but so far I just found a method which accepts a file (HTML2Slim.convert!(file, :erb)).

To get the ERB content programatically from Slim, you'd just do this:

require 'slim/erb_converter'
erb_source = Slim::ERBConverter.new.call(slim_source)

dmke avatar Dec 30 '14 21:12 dmke

Currently there is none. But a while ago I started to write one based on the slim parser. But I didn't finish it. Maybe someone else wants to do that.

slim/formatter.rb:

require 'slim'

module Slim
  # Slim formatter
  #
  # @example Conversion
  #   Slim::Formatter.new(options).call(slim_code) # outputs formatted slim code
  #
  # @api public
  class Formatter < Engine
    class Filter < ::Slim::Filter
      def call(exp)
        @indent = 0
        super.to_s
      end

      def on_multi(*exps)
        super[1..-1].join
      end

      def on_newline
        "\n"
      end

      def on_html_doctype(name)
        "#{indent}doctype #{name}"
      end

      def on_html_tag(name, attrs, content = nil)
        ret = "#{indent}#{name}#{compile attrs}"
        if content
          ret << ' ' << block(content)
        end
        ret
      end

      def block(content)
        @indent += 1
        compile(content).to_s # todo remove to_s
      ensure
        @indent -= 1
      end


      def on_html_attrs(*attrs)
        attrs.empty? ? '' : "(#{super[2..-1].join(' ')})"
      end

      def on_slim_interpolate(text)
        compile(text)
      end

      def on_slim_text(type, text)
        block(text)
      end

      def on_slim_embedded(type, text)
        "#{indent}#{block text}"
      end

      def on_html_comment(exp)
        "#{indent}/ #{block exp}"
      end

      def on_slim_control(line, block)
        "#{indent}- #{line}#{block block}"
      end

      def on_html_attr(name, content)
        "#{name}=#{compile content}"
      end

      def indent
        '  ' * @indent
      end

      #  @stacks.last << [:html, :condcomment, $1, block]
      #  @stacks.last << [:slim, :text, :verbatim, parse_text_block($', @indents.last + $2.size + 1)]
      #  @stacks.last << [:static, ' '] if trailing_ws
      #  @stacks.last << [:multi, [:slim, :interpolate, @line], block]
      #  @stacks.last << [:slim, :control, parse_broken_line, block]
      #  @stacks.last << [:slim, :output, $1.empty?, parse_broken_line, block]
      #  @stacks.last << [:slim, :embedded, $1, parse_text_block($', @orig_line.size - $'.size + $2.size)]
      #  @stacks.last << [:html, :doctype, $'.strip]
      #attributes = [:html, :attrs]
      #shortcut.each {|a| attributes << [:html, :attr, a, [:static, $2]] }
      #tag = [:html, :tag, tag, attributes]
      #  tag << [:slim, :text, :inline, parse_text_block($', @orig_line.size - $'.size)]
      #    attributes << [:slim, :splat, parse_ruby_code(delimiter)]
      #    attributes << [:html, :attr, name, [:slim, :attrvalue, escape, value]]
    end

    remove //
    filter :Encoding
    filter :RemoveBOM
    use Slim::Parser
    use Filter
  end
end

you could then add it to the command.rb

      opts.on('-f', '--format', 'Format Slim code') do
        @options[:format] = true
      end
  result =
        if @options[:format]
          require 'slim/formatter'
           Formatter.new(file: @options[:file]).call(@options[:input].read)
        elsif @options[:erb]
           ...

minad avatar Feb 25 '15 13:02 minad

would be really cool, if based on prettier and prettier/plugin-ruby

dorianmariecom avatar May 05 '21 11:05 dorianmariecom

seems like you could convert from slim to erb, then use prettier erb and then convert back to slim, just an idea

dorianmariecom avatar Sep 25 '21 08:09 dorianmariecom

https://github.com/prettier/plugin-pug Pug looks very similar to Slim. Might be a good starting point

mikker avatar Aug 07 '23 12:08 mikker