sept 1.4.0 → 1.4.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/bin/sept +92 -47
  3. data/lib/sept.rb +13 -18
  4. metadata +3 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 975454ccdb264c314891b01818ca7bdf7e5f84ab
4
- data.tar.gz: 66161ae4a0e167b286aaf34b3e68f5834ad1c349
3
+ metadata.gz: ff22d39bb85df25a6eb4869e661c2cd4fcb557c7
4
+ data.tar.gz: 5b94445b1b72e802435e19b118638d56fdd558df
5
5
  SHA512:
6
- metadata.gz: 7423c55788b587d66e3750398106c8b7aa36d0a13403d043a70d47f44888a5aa745cd27436f43528a68a237a4c4a7185e9fe4ff2abc8448a719c078d482947a5
7
- data.tar.gz: e7446316fbe8d43f9c0b7a12d6a344cfcc36d5b7f1011f52f67f8a63c23eda0c67267cb5beecde8e6cc77ae61a655442d873367d1403d033795adeef129abe80
6
+ metadata.gz: 63d5b3571bbad8f9ce26efef999127cb3e095f7b9c52ced1007c404d6dd9b25d813b2f483e038f6ffbe4c9619e112102e879721afbeb84347ff1aea789292e96
7
+ data.tar.gz: 344f70a7803b00d9ca08896acbbfc333c53d84ee899b92d907a8a4f8b7f044d138c76157cb9617b29094e33b93e0c9fc87d1ef0a8db2162b4cccd5e5724c1120
data/bin/sept CHANGED
@@ -1,61 +1,106 @@
1
1
  #!/usr/bin/env ruby
2
-
3
2
  %w(sept colorize json).each { |f| require f }
3
+ HELP_MESSAGE =<<-END
4
+ S-Expression Powered Template HyperText Markup Language
5
+ Author: Timur Ismagilov <bouncepaw2@ya.ru>
6
+ Repo: https://github.com/bouncepaw/sept
7
+ DESCRIPTION
8
+ This program parses SeptHTML — Lisp-inspired abstraction on HTML. Soon it
9
+ will support SeptCSS too. SEPT HTML Just like in Lisp, pages are written in
10
+ parenthesized syntax — S-expressions. Elements of sexpr are divided by space
11
+ character. Strings and other sexprs can be nested inside sexpr, except of the
12
+ first element — it must be string. HTML tags are expressed by sexpr with at
13
+ least one element. For example:
14
+ (br) -> <br>
15
+ (p Hello) -> <p>Hello</p>
16
+ (div (p Hello) (br)) -> <div><p>Hello</p><br></div>
17
+
18
+ If a string contains spaces, it must be surrounded by quotes. There is no
19
+ difference between double and single quotes. C-style escape sequences
20
+ are supported, I think. For example:
21
+ (title "Demo page") -> <title>Demo page</title>
22
+ (title 'Demo page') -> <title>Demo page</title>
23
+ (title "\"Demo page") -> <title>"Demo page</title>
24
+ (title Demo page) -> <title>Demopage</title>
25
+
26
+ So, second, third, fourth...last elements are children of the tag. To add
27
+ attribute to a tag, you need to write them as usual after tag name, but they
28
+ nust be enclosed by quotes. For example:
29
+ ("button onclick='buttonPressed()'" "Click this button")
30
+ -> <button onclick='buttonPressed()'>Click this button</button>
31
+
32
+ There is CSS-inspired shortcut for class and id attributes. Multiple classes
33
+ can be written, but only one id can be used and it must be placed after
34
+ classes. For example:
35
+ (p.text "This is text") -> <p class="text">This is text</p>
36
+ (p.the#best "This is best") -> <p class="the" id="best">This is best</p>
37
+ (p.one.two Hey) -> <p class="one two">Hey</p>
38
+
39
+ CLI UTILITY
40
+ After installing `sept` gem, `sept` utility is installed too. This section
41
+ covers it. Several arguments are understood by the program:
42
+
43
+ sept -v, sept --version
44
+ Prints version and exits.
45
+
46
+ sept -h, sept --help
47
+ Prints this message and exits.
48
+
49
+ sept -d <data> <files>, sept --data <data> <files>
50
+ You pass JSON hash to <data> for parser to use as parameters. You can
51
+ read more about parameters in the section above. <files> are filenames of
52
+ files, they must have one of extensions supported by Sept. For example:
53
+ $ sept -d {"name":"John Smith"} bio.septh not_bio
54
+ `bio.smith` will be parsed and saved in `bio.html`, `not_bio` will be
55
+ ignored and error message will be printed.
56
+
57
+ sept -f <json file> <files>, sept --file <json file> <files>
58
+ Same as above, but instead of JSON hash, you pass name of the file with
59
+ JSON hash. For example:
60
+ $ cat data
61
+ {"name":"John Smith"}
62
+ $ sept -f data bio.septh not_bio
63
+ `bio.smith` will be parsed and saved in `bio.html`, `not_bio` will be
64
+ ignored and error message will be printed.
65
+
66
+ sept <files>
67
+ Same as above, but no parameters are passed. If any of the files you
68
+ passed needs it, error message will be printed and the program will stop.
69
+ END
70
+ def validate_list_of_files(list_of_files)
71
+ correct_ones = []
72
+ list_of_files.each do |file|
73
+ if %w(.septh .septc).include? file
74
+ correct_ones << file
75
+ else
76
+ puts "ERROR #{file} is not a Sept file. Use `.septh` or `.septc`".red
77
+ end
78
+ end
79
+ end
4
80
 
5
81
  if ARGV.length == 0
6
82
  puts "ERROR No arguments passed!".red
83
+ exit 1
7
84
  else
8
85
  case ARGV[0]
9
86
  when '-h', '--help' # help
10
- puts <<-TEXT
11
- S-Expression Powered Template HyperText Markup Language
12
-
13
- EXAMPLE
14
- test.sept:
15
- (html
16
- (head
17
- (title Example)
18
- (style ".heading { color: red }"))
19
- (body
20
- ("h1 class='heading'" Example)
21
- (p %{param})))
22
-
23
- After running `sept -d {"param":"Paragraph"} test.sept` you will get
24
- test.html file:
25
- <html>
26
- <head>
27
- <title>Example</title>
28
- <style>.heading { color: red }</style>
29
- </head>
30
- <body>
31
- <h1 class="heading">Example</h1>
32
- <p>Paragraph</p>
33
- </body>
34
- </html>
35
-
36
- SHELL ARGUMENTS
37
- sept -v, sept --version
38
- Prints version and exits.
39
- sept -h, sept --help
40
- Prints this message and exit.
41
- sept -d <data> <files>, sept --data <data> <files>
42
- You pass JSON hash to <data> for parser to use as parameters.
43
- <files> are filenames of files.
44
- sept -f <json file> <files>, sept --file <json file> <files>
45
- Same as above, but instead of JSON hash, you pass name name of the file
46
- with JSON hash.
47
- TEXT
87
+ puts HELP_MESSAGE
48
88
  when '-v', '--version'
49
89
  # TODO: fetch version from sept.gemspec automatically
50
- puts "SEPT HTML version 1.4.0"
90
+ puts "SEPT HTML version 1.4.2"
51
91
  when '-d', '--data'
52
- puts "Passed data as argument"
53
- sept = Sept.new JSON.parse(ARGV[1]), ARGV[2..-1]
54
- when '-f', '-file'
55
- puts "Passed data as file"
56
- sept = Sept.new JSON.parse(File.read ARGV[1]), ARGV[2..-1]
92
+ puts "Passed data as argument".light_blue
93
+ params = JSON.parse ARGV[1]
94
+ list_of_files = validate_list_of_files ARGV[2..-1]
95
+ when '-f', '--file'
96
+ puts "Passed data as file".light_blue
97
+ params = JSON.parse File.read ARGV[1]
98
+ list_of_files = validate_list_of_files ARGV[2..-1]
57
99
  else
58
- puts "No passed data"
59
- sept = Sept.new({}, ARGV)
100
+ puts "No passed data".light_blue
101
+ params = {}
102
+ list_of_files = validate_list_of_files ARGV
60
103
  end
61
104
  end
105
+
106
+ sept = Sept.new(params, list_of_files)
data/lib/sept.rb CHANGED
@@ -28,7 +28,7 @@ class Sept
28
28
  # @param params [Hash] Hash with parameters.
29
29
  # @params files_to_parse [Array] List of files program will try to parse
30
30
  def initialize(params, files_to_parse)
31
- @params = params.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo}
31
+ @params = params
32
32
  @html = ''
33
33
 
34
34
  files_to_parse.each { |f| self.cook f }
@@ -39,38 +39,34 @@ class Sept
39
39
  # @param file [String] Sept file
40
40
  # @return [String] HTML file
41
41
  def generate(file)
42
- self.to_html(self.prepare SXP.read file) % @params
42
+ self.to_html self.prepare SXP.read file % @params
43
43
  end
44
44
 
45
45
  # Function that 'cooks' passed file. It logs some info. The name is bad
46
46
  #
47
47
  # @param filename [String] Name of file to 'cook'
48
48
  def cook(filename)
49
- unless File.extname(filename) == ".sept"
50
- puts "ERROR #{filename} is not `.sept` file, so can't parse it!".red
51
- return
52
- end
53
49
  @html = ''
54
50
 
55
51
  file = File.read filename
56
- puts "Got #{filename}, its content is:\n#{file}"
52
+ puts "Got #{filename}:\n#{file}"
57
53
 
58
- new_filename = filename[0..-6] + ".html"
59
- new_file = self.generate file
54
+ filename = filename[0..-5] + 'html'
55
+ file = self.generate file
60
56
 
61
57
  # Create file if it does not exist yet
62
- File.new new_filename, 'w+' unless File.file? new_filename
63
- File.write new_filename, new_file
64
- puts "Parsed #{filename} and saved in #{new_filename}:\n #{new_file}"
58
+ File.new filename, 'w+' unless File.file? filename
59
+ File.write filename, file
60
+ puts "Parsed and saved in #{filename}:\n#{file}"
65
61
  end
66
62
 
67
63
  # Recursive function that turns everything in `node` to a string
68
64
  #
69
65
  # @param node [Array, String] A Sept node
70
- def prepare(node)
66
+ def self.prepare(node)
71
67
  node.each_with_index do |sub, i|
72
68
  if sub.is_a? Array
73
- self.prepare sub
69
+ Sept.prepare sub
74
70
  else
75
71
  node[i] = sub.to_s
76
72
  end
@@ -91,7 +87,7 @@ class Sept
91
87
  node = []
92
88
  node[0] = file
93
89
  else
94
- temp = self.unfold_tag node[0]
90
+ temp = Sept.unfold_tag node[0]
95
91
  @html << "<#{temp}>"
96
92
  node[1..-1].each { |e| self.to_html e }
97
93
  @html << "</#{temp.split(' ')[0]}>"
@@ -111,7 +107,7 @@ class Sept
111
107
  #
112
108
  # @param tag [String] String like `tag.class.class2#id other-arg=""`
113
109
  # @return [String] Unfolded tag
114
- def unfold_tag(tag)
110
+ def self.unfold_tag(tag)
115
111
  halves = tag.split(' ')
116
112
  half = halves[0]
117
113
 
@@ -129,8 +125,7 @@ class Sept
129
125
  half = temp[0]
130
126
  end
131
127
 
132
- tag_name = half
133
- ret = tag_name
128
+ ret = half
134
129
  ret << " class='#{klass}'" unless klass.empty?
135
130
  ret << " id='#{id}'" unless id.empty?
136
131
  ret << " #{halves[1]}" unless halves[1].nil?
metadata CHANGED
@@ -1,18 +1,17 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sept
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.0
4
+ version: 1.4.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Timur Ismagilov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-10-04 00:00:00.000000000 Z
11
+ date: 2017-10-08 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: |2
14
- Write your HTML pages like Lisp code. Support of included files,
15
- parameters. CLI utility provided. Run `sept -h` for more info
14
+ Write your HTML pages like Lisp code. CLI utility. Run `sept -h` for info
16
15
 
17
16
  (html
18
17
  (head