Skip to content

Commit

Permalink
Fix combiner logic, fix watch-mode and change --silent to --verbose
Browse files Browse the repository at this point in the history
  • Loading branch information
SEbbaDK committed Mar 8, 2022
1 parent 3ede15c commit 2d7201f
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 30 deletions.
42 changes: 24 additions & 18 deletions combiner.cr
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,20 @@ module Combiner
Regex
end

def cat_file_from_to(from, to, file : Path, include_edges = true, search_mode = SearchMode::Equality)
def match?(search_mode : SearchMode, a, b)
case search_mode
when SearchMode::Equality
a == b
when SearchMode::SubString
a.includes? b
when SearchMode::Regex
a =~ b
else
raise Exception.new "Internal Error: Unknown SearchMode"
end
end

def cat_file_from_to(from, to, file : Path, search_mode : SearchMode, include_edges = true)
result = ""
in_block = false
found = false
Expand All @@ -61,26 +74,19 @@ module Combiner
File.each_line file do |line|
begin
stripline = line.rstrip()
case search_mode
when SearchMode::Equality
in_block = true if stripline == from
when SearchMode::SubString
in_block = true if stripline.includes? from
when SearchMode::Regex
in_block = true if stripline =~ from
end
starting = match? search_mode, stripline, from
ending = match? search_mode, stripline, to

in_block = true if starting && include_edges && !found
in_block = false if in_block && ending && !include_edges

result += expand_line(file, line) + '\n' if in_block

in_block = true if starting && !found
in_block = false if ending && found

found = true if in_block

case search_mode
when SearchMode::Equality
in_block = false if stripline == to
when SearchMode::SubString
in_block = false if stripline.includes? to
when SearchMode::Regex
in_block = false if stripline =~ to
end
rescue ex : CompileException
raise ex.add_stack file, l
end
Expand Down Expand Up @@ -152,7 +158,7 @@ module Combiner
raise CompileException.new "#{line}: No module name given for kmodule selector: #{selector}"
else
modulename = split[1]
return cat_file_from_to "module #{modulename}", "endmodule", filename
return cat_file_from_to "module #{modulename}", "endmodule", filename, search_mode = SearchMode::Equality
end

else
Expand Down
4 changes: 2 additions & 2 deletions explorer.cr
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ module Explorer
match = /€\[([^]]+)\]/.match(line)
if ! match.nil?
path = Path[match[1]]
puts "Found #{path}"
#puts "Found #{path}"
if ! path.absolute?
path = (Path[file.dirname] / path).normalize
end
puts "Adding as #{path}"
#puts "Adding as #{path}"
files << path
files += explore(path)
end
Expand Down
13 changes: 8 additions & 5 deletions marka-cli.cr
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ OptionParser.parse do |p|
end
end

p.on("-s", "--silent", "Disables printing status messages") do
marka.silent = true
p.on("-v", "--verbose", "Enables printing status messages") do
marka.silent = false
end

p.on("-l", "--latex", "Outputs latex instead of rending to a pdf file") do
Expand Down Expand Up @@ -113,7 +113,12 @@ else
watchers = files.map do |file|
Inotify.watch file.to_s do |event|
if event.type.modify?
channel.send(file)
#puts "#{file} was modified"
begin
channel.send(file)
rescue Channel::ClosedError
# Just do nothing when the channel is closed out
end
end
end
end
Expand All @@ -127,8 +132,6 @@ else
channel.close

puts "Rerendering because #{change} changed"

render marka, target
end
end

8 changes: 4 additions & 4 deletions marka.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ require "./combiner"

class Marka
property filters = [] of Path
property silent = false
property silent = true
property latex_output = false
property beamer_output = false
property output_file = "result.pdf"
Expand All @@ -11,15 +11,15 @@ class Marka
property extra_pandoc_args = [] of String

def render(file)
puts "Running Combiner on #{file}" if ! silent
puts "Running Combiner on #{file}" unless silent
input = Combiner.combine file

if ! bibliography.nil?
puts "Adding bibliography header" if ! silent
puts "Adding bibliography header" unless silent
input += "\n# Bibliography\n"
end

puts "Running Pandoc" if ! silent
puts "Running Pandoc" unless silent
pipe = IO::Memory.new input
if latex_output
output = "--to=latex"
Expand Down
2 changes: 1 addition & 1 deletion shard.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: marka
version: 0.2.0
version: 0.3.0

authors:
- sebbadk <[email protected]>
Expand Down

0 comments on commit 2d7201f

Please sign in to comment.