Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply all standard fixes #926

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name: StandardRB

on: [push]

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: StandardRB Linter
uses: andrewmcodes/[email protected]
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }
15 changes: 8 additions & 7 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,22 @@ source 'https://rubygems.org/'

gemspec

gem 'test-unit', '~> 3.5'
gem 'kramdown', '~> 2.4'
gem 'minitest', '~> 5.15'
gem 'rake', '~> 13.0'
gem 'kramdown', '~> 2.4'
gem 'standard', '~> 1.29'
gem 'test-unit', '~> 3.5'

if ENV['TEMPLE'] && ENV['TEMPLE'] != 'master'
gem 'temple', "= #{ENV['TEMPLE']}"
else
# Test against temple master by default
gem 'temple', :github => 'judofyr/temple'
gem 'temple', github: 'judofyr/temple'
end

if ENV['TILT']
if ENV['TILT'] == 'master'
gem 'tilt', :github => 'rtomayko/tilt'
gem 'tilt', github: 'rtomayko/tilt'
else
gem 'tilt', "= #{ENV['TILT']}"
end
Expand All @@ -27,7 +28,7 @@ if ENV['RAILS']

# we need some smarter test logic for the different Rails versions
if ENV['RAILS'] == 'main'
gem 'rails', :github => 'rails/rails', branch: 'main'
gem 'rails', github: 'rails/rails', branch: 'main'
else
gem 'rails', "= #{ENV['RAILS']}"
end
Expand All @@ -37,8 +38,8 @@ if ENV['SINATRA']
gem 'rack-test'

if ENV['SINATRA'] == 'master'
gem 'sinatra', :github => 'sinatra/sinatra'
gem 'sinatra', github: 'sinatra/sinatra'
else
gem 'sinatra', :tag => "v#{ENV['SINATRA']}"
gem 'sinatra', tag: "v#{ENV['SINATRA']}"
end
end
70 changes: 35 additions & 35 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,80 +1,80 @@
begin
require 'bundler/setup'
require "bundler/setup"
Bundler::GemHelper.install_tasks
rescue Exception
end

require 'rake/testtask'
require "rake/testtask"

task 'test' => %w(test:core test:literate test:logic_less test:translator test:smart test:include)
task "test" => %w[test:core test:literate test:logic_less test:translator test:smart test:include]

namespace 'test' do
Rake::TestTask.new('core') do |t|
t.libs << 'lib' << 'test/core'
t.test_files = FileList['test/core/test_*.rb']
namespace "test" do
Rake::TestTask.new("core") do |t|
t.libs << "lib" << "test/core"
t.test_files = FileList["test/core/test_*.rb"]
t.warning = true
#t.ruby_opts << '-w' << '-v'
# t.ruby_opts << '-w' << '-v'
end

Rake::TestTask.new('literate') do |t|
t.libs << 'lib' << 'test/literate'
t.test_files = FileList['test/literate/run.rb']
Rake::TestTask.new("literate") do |t|
t.libs << "lib" << "test/literate"
t.test_files = FileList["test/literate/run.rb"]
t.warning = true
end

Rake::TestTask.new('logic_less') do |t|
t.libs << 'lib' << 'test/core'
t.test_files = FileList['test/logic_less/test_*.rb']
Rake::TestTask.new("logic_less") do |t|
t.libs << "lib" << "test/core"
t.test_files = FileList["test/logic_less/test_*.rb"]
t.warning = true
end

Rake::TestTask.new('translator') do |t|
t.libs << 'lib' << 'test/core'
t.test_files = FileList['test/translator/test_*.rb']
Rake::TestTask.new("translator") do |t|
t.libs << "lib" << "test/core"
t.test_files = FileList["test/translator/test_*.rb"]
t.warning = true
end

Rake::TestTask.new('smart') do |t|
t.libs << 'lib' << 'test/core'
t.test_files = FileList['test/smart/test_*.rb']
Rake::TestTask.new("smart") do |t|
t.libs << "lib" << "test/core"
t.test_files = FileList["test/smart/test_*.rb"]
t.warning = true
end

Rake::TestTask.new('include') do |t|
t.libs << 'lib' << 'test/core'
t.test_files = FileList['test/include/test_*.rb']
Rake::TestTask.new("include") do |t|
t.libs << "lib" << "test/core"
t.test_files = FileList["test/include/test_*.rb"]
t.warning = true
end

Rake::TestTask.new('rails') do |t|
t.libs << 'lib'
t.test_files = FileList['test/rails/test/test_*.rb']
Rake::TestTask.new("rails") do |t|
t.libs << "lib"
t.test_files = FileList["test/rails/test/test_*.rb"]
t.warning = true
end

Rake::TestTask.new('sinatra') do |t|
t.libs << 'lib'
t.test_files = FileList['test/sinatra/test_*.rb']
Rake::TestTask.new("sinatra") do |t|
t.libs << "lib"
t.test_files = FileList["test/sinatra/test_*.rb"]

# Copied from test task in Sinatra project to mimic their approach
t.ruby_opts = ['-r rubygems'] if defined? Gem
t.ruby_opts << '-I.'
t.ruby_opts = ["-r rubygems"] if defined? Gem
t.ruby_opts << "-I."
t.warning = true
end
end

begin
require 'yard'
require "yard"
YARD::Rake::YardocTask.new do |t|
t.files = %w(lib/**/*.rb)
t.files = %w[lib/**/*.rb]
end
rescue LoadError
task :yard do
abort 'YARD is not available. In order to run yard, you must: gem install yard'
abort "YARD is not available. In order to run yard, you must: gem install yard"
end
end

desc "Generate Documentation"
task doc: :yard

task default: 'test'
task default: "test"
31 changes: 16 additions & 15 deletions lib/slim.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
# frozen_string_literal: true
require 'temple'
require 'slim/parser'
require 'slim/filter'
require 'slim/do_inserter'
require 'slim/end_inserter'
require 'slim/embedded'
require 'slim/interpolation'
require 'slim/controls'
require 'slim/splat/filter'
require 'slim/splat/builder'
require 'slim/code_attributes'
require 'slim/engine'
require 'slim/template'
require 'slim/version'
require 'slim/railtie' if defined?(Rails::Railtie)

require "temple"
require "slim/parser"
require "slim/filter"
require "slim/do_inserter"
require "slim/end_inserter"
require "slim/embedded"
require "slim/interpolation"
require "slim/controls"
require "slim/splat/filter"
require "slim/splat/builder"
require "slim/code_attributes"
require "slim/engine"
require "slim/template"
require "slim/version"
require "slim/railtie" if defined?(Rails::Railtie)
33 changes: 17 additions & 16 deletions lib/slim/code_attributes.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# frozen_string_literal: true

module Slim
# @api private
class CodeAttributes < Filter
Expand All @@ -9,7 +10,7 @@ class CodeAttributes < Filter
# @param [Array] attrs Array of temple expressions
# @return [Array] Compiled temple expression
def on_html_attrs(*attrs)
[:multi, *attrs.map {|a| compile(a) }]
[:multi, *attrs.map { |a| compile(a) }]
end

# Handle attribute expression `[:html, :attr, name, value]`
Expand All @@ -22,18 +23,18 @@ def on_html_attr(name, value)
# We handle the attribute as a boolean attribute
escape, code = value[2], value[3]
case code
when 'true'
when "true"
[:html, :attr, name, [:multi]]
when 'false', 'nil'
when "false", "nil"
[:multi]
else
tmp = unique_name
[:multi,
[:code, "#{tmp} = #{code}"],
[:if, tmp,
[:if, "#{tmp} == true",
[:html, :attr, name, [:multi]],
[:html, :attr, name, [:escape, escape, [:dynamic, tmp]]]]]]
[:code, "#{tmp} = #{code}"],
[:if, tmp,
[:if, "#{tmp} == true",
[:html, :attr, name, [:multi]],
[:html, :attr, name, [:escape, escape, [:dynamic, tmp]]]]]]
end
else
# Attribute with merging
Expand All @@ -52,14 +53,14 @@ def on_slim_attrvalue(escape, code)
if delimiter = options[:merge_attrs][@attr]
tmp = unique_name
[:multi,
[:code, "#{tmp} = #{code}"],
[:if, "Array === #{tmp}",
[:multi,
[:code, "#{tmp} = #{tmp}.flatten"],
[:code, "#{tmp}.map!(&:to_s)"],
[:code, "#{tmp}.reject!(&:empty?)"],
[:escape, escape, [:dynamic, "#{tmp}.join(#{delimiter.inspect})"]]],
[:escape, escape, [:dynamic, tmp]]]]
[:code, "#{tmp} = #{code}"],
[:if, "Array === #{tmp}",
[:multi,
[:code, "#{tmp} = #{tmp}.flatten"],
[:code, "#{tmp}.map!(&:to_s)"],
[:code, "#{tmp}.reject!(&:empty?)"],
[:escape, escape, [:dynamic, "#{tmp}.join(#{delimiter.inspect})"]]],
[:escape, escape, [:dynamic, tmp]]]]
else
[:escape, escape, [:dynamic, code]]
end
Expand Down
48 changes: 24 additions & 24 deletions lib/slim/command.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true
require 'slim'
require 'optparse'

require "slim"
require "optparse"

module Slim
Engine.set_options pretty: false
Expand All @@ -24,56 +25,56 @@ def run

# Configure OptionParser
def set_opts(opts)
opts.on('-s', '--stdin', 'Read input from standard input instead of an input file') do
opts.on("-s", "--stdin", "Read input from standard input instead of an input file") do
@options[:input] = $stdin
end

opts.on('--trace', 'Show a full traceback on error') do
opts.on("--trace", "Show a full traceback on error") do
@options[:trace] = true
end

opts.on('-c', '--compile', 'Compile only but do not run') do
opts.on("-c", "--compile", "Compile only but do not run") do
@options[:compile] = true
end

opts.on('-e', '--erb', 'Convert to ERB') do
opts.on("-e", "--erb", "Convert to ERB") do
@options[:erb] = true
end

opts.on('--rails', 'Generate rails compatible code (Implies --compile)') do
opts.on("--rails", "Generate rails compatible code (Implies --compile)") do
Engine.set_options disable_capture: true, generator: Temple::Generators::RailsOutputBuffer
@options[:compile] = true
end

opts.on('-r', '--require library', "Load library or plugin with -r slim/plugin") do |lib|
opts.on("-r", "--require library", "Load library or plugin with -r slim/plugin") do |lib|
require lib.strip
end

opts.on('-p', '--pretty', 'Produce pretty html') do
opts.on("-p", "--pretty", "Produce pretty html") do
Engine.set_options pretty: true
end

opts.on('-o', '--option name=code', String, 'Set slim option') do |str|
parts = str.split('=', 2)
Engine.options[parts.first.gsub(/\A:/, '').to_sym] = eval(parts.last)
opts.on("-o", "--option name=code", String, "Set slim option") do |str|
parts = str.split("=", 2)
Engine.options[parts.first.gsub(/\A:/, "").to_sym] = eval(parts.last)
end

opts.on('-l', '--locals Hash|YAML|JSON', String, 'Set local variables') do |locals|
opts.on("-l", "--locals Hash|YAML|JSON", String, "Set local variables") do |locals|
@options[:locals] =
if locals =~ /\A\s*\{\s*\p{Word}+:/
if /\A\s*\{\s*\p{Word}+:/.match?(locals)
eval(locals)
else
require 'yaml'
require "yaml"
YAML.load(locals)
end
end

opts.on_tail('-h', '--help', 'Show this message') do
opts.on_tail("-h", "--help", "Show this message") do
puts opts
exit
end

opts.on_tail('-v', '--version', 'Print version') do
opts.on_tail("-v", "--version", "Print version") do
puts "Slim #{VERSION}"
exit
end
Expand All @@ -86,34 +87,33 @@ def process
file = args.shift
if file
@options[:file] = file
@options[:input] = File.open(file, 'r')
@options[:input] = File.open(file, "r")
else
@options[:file] = 'STDIN'
@options[:file] = "STDIN"
@options[:input] = $stdin
end
end

locals = @options.delete(:locals) || {}
result =
if @options[:erb]
require 'slim/erb_converter'
require "slim/erb_converter"
ERBConverter.new(file: @options[:file]).call(@options[:input].read)
elsif @options[:compile]
Engine.new(file: @options[:file]).call(@options[:input].read)
else
Template.new(@options[:file]) { @options[:input].read }.render(nil, locals)
end

rescue Exception => ex
raise ex if @options[:trace] || SystemExit === ex
$stderr.print "#{ex.class}: " if ex.class != RuntimeError
$stderr.puts ex.message
$stderr.puts ' Use --trace for backtrace.'
warn ex.message
warn " Use --trace for backtrace."
exit 1
else
unless @options[:output]
file = args.shift
@options[:output] = file ? File.open(file, 'w') : $stdout
@options[:output] = file ? File.open(file, "w") : $stdout
end
@options[:output].puts(result)
exit 0
Expand Down
Loading