Skip to content

Commit

Permalink
crystal tool format
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromegn committed Mar 13, 2016
1 parent e45c205 commit 5557664
Show file tree
Hide file tree
Showing 19 changed files with 40 additions and 70 deletions.
2 changes: 1 addition & 1 deletion examples/basic.cr
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ strings = ["ah", "oh"]

res = Slang.process_file("#{__DIR__}/../spec/fixtures/basic.slang")

puts res
puts res
19 changes: 8 additions & 11 deletions spec/slang/parser_spec.cr
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
require "../spec_helper"

describe Slang::Parser do

describe "tags" do

# it "parses simple tags correctly" do
# Slang.process_string("div").should eq "__slang__ << \"<div\"
# __slang__ << \">\"
# __slang__ << \"</div>\"
# __slang__ << \"\n\"
# "
# end
# it "parses simple tags correctly" do
# Slang.process_string("div").should eq "__slang__ << \"<div\"
# __slang__ << \">\"
# __slang__ << \"</div>\"
# __slang__ << \"\n\"
# "
# end

end

end
end
8 changes: 0 additions & 8 deletions spec/slang_spec.cr
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
require "./spec_helper"

describe Slang do

it "renders a basic document" do
res = render_file("spec/fixtures/basic.slang")
res.should eq <<-HTML
Expand Down Expand Up @@ -45,7 +44,6 @@ describe Slang do
end

describe "attributes" do

it "accepts string values" do
render("span attr=\"hello\"").should eq <<-HTML
<span attr="hello"></span>
Expand All @@ -57,11 +55,9 @@ describe Slang do
<span attr="hello world"></span>
HTML
end

end

describe "output" do

it "accepts spaces in attribute string values" do
res = render_file "spec/fixtures/output.slang"

Expand Down Expand Up @@ -89,11 +85,9 @@ describe Slang do
<div><ah></div>
HTML
end

end

describe "raw html" do

it "renders html" do
res = render_file "spec/fixtures/with_html.slang"

Expand All @@ -103,7 +97,5 @@ describe Slang do
</table>
HTML
end

end

end
2 changes: 1 addition & 1 deletion spec/spec_helper.cr
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ macro render(slang)
String.build do |__str__|
\{{ run("./support/process", {{slang}}, "__str__") }}
end
end
end
2 changes: 1 addition & 1 deletion spec/support/process.cr
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
require "../../src/slang"
puts Slang.process_string(ARGV[0], "dummy.slang", ARGV[1])
puts Slang.process_string(ARGV[0], "dummy.slang", ARGV[1])
2 changes: 1 addition & 1 deletion spec/support/process_file.cr
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
require "../../src/slang"
puts Slang.process_file(ARGV[0], ARGV[1])
puts Slang.process_file(ARGV[0], ARGV[1])
4 changes: 2 additions & 2 deletions src/slang.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require "./slang/lexer"
require "./slang/parser"
require "./slang/token"
require "./slang/macros"

# require "./slang/*"

module Slang
Expand All @@ -20,5 +21,4 @@ module Slang
raise "Slang template: #{filename} doesn't exist." unless File.exists?(filename)
process_string(File.read(filename), filename, buffer_name)
end

end
end
3 changes: 1 addition & 2 deletions src/slang/document.cr
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
module Slang
class Document < Node

def initialize
@token = Token.new
@token.column_number = 1
Expand All @@ -11,4 +10,4 @@ module Slang
true
end
end
end
end
8 changes: 2 additions & 6 deletions src/slang/lexer.cr
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
module Slang
class Lexer

getter token

def initialize(string)
Expand Down Expand Up @@ -77,9 +76,7 @@ module Slang
break
end
end

end

private def consume_element_attributes
current_attr_name = ""

Expand Down Expand Up @@ -236,7 +233,7 @@ module Slang
CLOSE_OPEN_MAP = {
'}' => '{',
']' => '[',
')' => '('
')' => '(',
}

private def consume_value(end_on_space = true)
Expand Down Expand Up @@ -336,6 +333,5 @@ module Slang
private def unexpected_char(char = current_char)
raise "unexpected char '#{char}'"
end

end
end
end
4 changes: 1 addition & 3 deletions src/slang/macros.cr
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
module Slang

macro embed(filename, io_name)
\{{ run("slang/slang/process", {{filename}}, {{io_name.id.stringify}}) }}
end
Expand All @@ -10,5 +9,4 @@ module Slang
embed_slang {{filename}}, "__slang__"
end
end

end
end
4 changes: 1 addition & 3 deletions src/slang/node.cr
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
module Slang
abstract class Node

getter :parent, :token
delegate :value, :column_number, :line_number, :escaped, :inline, @token

Expand Down Expand Up @@ -47,8 +46,7 @@ module Slang
node.to_s(str, buffer_name)
end
end

end
end

require "./nodes/*"
require "./nodes/*"
4 changes: 1 addition & 3 deletions src/slang/nodes/comment.cr
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
module Slang
module Nodes
class Comment < Node

delegate :conditional, :visible, @token

def to_s(str, buffer_name)
Expand All @@ -25,7 +24,6 @@ module Slang
def conditional?
!conditional.empty?
end

end
end
end
end
4 changes: 1 addition & 3 deletions src/slang/nodes/control.cr
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
module Slang
module Nodes
class Control < Node

def to_s(str, buffer_name)
str << "#{value}\n"
if children?
Expand All @@ -11,7 +10,6 @@ module Slang
str << "end\n"
end
end

end
end
end
end
2 changes: 1 addition & 1 deletion src/slang/nodes/doctype.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ module Slang
end
end
end
end
end
8 changes: 3 additions & 5 deletions src/slang/nodes/element.cr
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
module Slang
module Nodes
class Element < Node

SELF_CLOSING_TAGS = ["area", "base", "br", "col", "embed", "hr", "img", "input", "keygen", "link", "menuitem", "meta", "param", "source", "track", "wbr"]
RAW_TEXT_TAGS = ["script", "style"]
RAW_TEXT_TAGS = ["script", "style"]

delegate :name, :id, :class_names, :attributes, @token

Expand Down Expand Up @@ -48,13 +47,12 @@ module Slang
end

def only_inline_children?
nodes.all? {|n| n.inline }
nodes.all? { |n| n.inline }
end

def self_closing?
SELF_CLOSING_TAGS.includes?(name)
end

end
end
end
end
3 changes: 1 addition & 2 deletions src/slang/nodes/text.cr
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ module Slang
end
str << ".to_s(#{buffer_name})\n"
end

end
end
end
end
21 changes: 10 additions & 11 deletions src/slang/parser.cr
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
module Slang
class Parser

def initialize(string)
@lexer = Lexer.new(string)
next_token
Expand All @@ -27,15 +26,15 @@ module Slang
end

node = case token.type
when :ELEMENT
Nodes::Element.new(parent, token)
when :CONTROL
Nodes::Control.new(parent, token)
when :COMMENT
Nodes::Comment.new(parent, token)
else
Nodes::Text.new(parent, token)
end
when :ELEMENT
Nodes::Element.new(parent, token)
when :CONTROL
Nodes::Control.new(parent, token)
when :COMMENT
Nodes::Comment.new(parent, token)
else
Nodes::Text.new(parent, token)
end
parent.not_nil!.nodes << node
@current_node = node
next_token
Expand All @@ -54,4 +53,4 @@ module Slang
raise "unexpected token '#{token}'"
end
end
end
end
2 changes: 1 addition & 1 deletion src/slang/process.cr
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
require "slang"
puts Slang.process_file(ARGV[0], ARGV[1])
puts Slang.process_file(ARGV[0], ARGV[1])
8 changes: 3 additions & 5 deletions src/slang/token.cr
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
module Slang
class Token

property :type
property :line_number, :column_number

# elements
property :name,
:class_names,
:attributes,
:id
:class_names,
:attributes,
:id

property :value, :escaped, :inline, :visible, :conditional

Expand Down

0 comments on commit 5557664

Please sign in to comment.