Module: IsoDoc::GemTasks

Extended by:
Rake::DSL
Defined in:
lib/isodoc/gem_tasks.rb

Constant Summary collapse

@@css_list =
[]

Class Method Summary collapse

Class Method Details

.comment_out_liquid(text) ⇒ Object



84
85
86
87
88
89
90
91
92
# File 'lib/isodoc/gem_tasks.rb', line 84

def comment_out_liquid(text)
  text.split("\n").map do |line|
    if line.match?(/(?<!{){({|%)(?![{%]).+(}|%)}/)
      "/* LIQUID_COMMENT#{line}LIQUID_COMMENT */"
    else
      line
    end
  end.join("\n")
end

.compile_scss(filename) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/isodoc/gem_tasks.rb', line 113

def compile_scss(filename)
  load_paths = load_scss_paths(filename)
  Dir.mktmpdir do |dir|
    File.write(File.join(dir, "variables.scss"), fonts_placeholder)
    sheet_content = File.read(filename, encoding: "UTF-8")
      # Drop the m flag and use [^{}\n]* so the lookahead is bounded to
      # one line, preventing O(n^2) scanning across multi-line inputs.
      .gsub(/([a-z])\.([0-9])(?=[^{}\n]*{)/, "\\1.__WORD__\\2")
    SassC::Engine.new(%<@use "variables" as *;\n#{sheet_content}>,
                      syntax: :scss, quiet_deps: true,
                      load_paths: load_paths + [dir],
                      importer: SasscImporter)
      .render.gsub("__WORD__", "")
  end
end

.compile_scss_task(current_task) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
# File 'lib/isodoc/gem_tasks.rb', line 140

def compile_scss_task(current_task)
  filename = current_task.source
  basename = File.basename(filename, ".*")
  compiled_path = File.join(File.dirname(filename), "#{basename}.css")
  content = uncomment_out_liquid(compile_scss(filename))
  File.open(compiled_path, "w:UTF-8") do |f|
    f.write(content)
  end
  @@css_list << compiled_path
  CLEAN << compiled_path
end

.css_list ⇒ Object



16
17
18
# File 'lib/isodoc/gem_tasks.rb', line 16

def self.css_list
  @@css_list
end

.fonts_placeholder ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
# File 'lib/isodoc/gem_tasks.rb', line 101

def fonts_placeholder
  <<~TEXT
    $bodyfont: "{{bodyfont}}";
    $headerfont: "{{headerfont}}";
    $monospacefont: "{{monospacefont}}";
    $normalfontsize: "{{normalfontsize}}";
    $smallerfontsize: "{{smallerfontsize}}";
    $footnotefontsize: "{{footnotefontsize}}";
    $monospacefontsize: "{{monospacefontsize}}";
  TEXT
end

.git_cache_compiled_files ⇒ Object



63
64
65
66
67
# File 'lib/isodoc/gem_tasks.rb', line 63

def git_cache_compiled_files
  @@css_list.each do |css_file|
    sh "git add #{css_file}"
  end
end

.git_rm_compiled_files ⇒ Object



69
70
71
72
73
# File 'lib/isodoc/gem_tasks.rb', line 69

def git_rm_compiled_files
  @@css_list.each do |css_file|
    sh "git rm --cached #{css_file}"
  end
end

.install ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/isodoc/gem_tasks.rb', line 20

def install
  rule ".css" => [proc { |tn| tn.sub(/\.css$/, ".scss") }] do |current_task|
    puts(current_task)
    compile_scss_task(current_task)
  rescue StandardError => e
    notify_broken_compilation(e, current_task)
  end

  scss_files = Rake::FileList["lib/**/*.scss"]
  source_files = scss_files.ext(".css")

  task :comment_out_liquid do
    process_css_files(scss_files) do |file_name|
      comment_out_liquid(File.read(file_name, encoding: "UTF-8"))
    end
  end

  task build_scss: [:comment_out_liquid].push(*source_files) do
    process_css_files(scss_files) do |file_name|
      uncomment_out_liquid(File.read(file_name, encoding: "UTF-8"))
    end
    git_cache_compiled_files && puts("Built scss!")
  end

  Rake::Task["build"].enhance [:build_scss] do
    git_rm_compiled_files
    Rake::Task[:clean].invoke
  end
end

.interactive? ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/isodoc/gem_tasks.rb', line 50

def interactive?
  ENV["CI"] == nil
end

.load_scss_paths(filename) ⇒ Object



129
130
131
132
133
134
135
136
137
138
# File 'lib/isodoc/gem_tasks.rb', line 129

def load_scss_paths(filename)
  require "sassc-embedded"
  require "isodoc/sassc_importer"
  isodoc_path = if Gem.loaded_specs["isodoc"]
                  File.join(Gem.loaded_specs["isodoc"].full_gem_path,
                            "lib", "isodoc")
                else File.join("lib", "isodoc")
                end
  [isodoc_path, File.dirname(filename)]
end

.notify_broken_compilation(error, current_task) ⇒ Object



54
55
56
57
58
59
60
61
# File 'lib/isodoc/gem_tasks.rb', line 54

def notify_broken_compilation(error, current_task)
  puts("Cannot compile #{current_task} because of #{error.message}")
  return unless interactive?

  puts("continue anyway[y|n]?")
  answer = $stdin.gets.strip
  exit(0) unless %w[y yes].include?(answer.strip.downcase)
end

.process_css_files(scss_files) ⇒ Object



75
76
77
78
79
80
81
82
# File 'lib/isodoc/gem_tasks.rb', line 75

def process_css_files(scss_files)
  scss_files.each do |file_name|
    result = yield(file_name)
    File.open(file_name, "w", encoding: "UTF-8") do |file|
      file.puts(result)
    end
  end
end

.uncomment_out_liquid(text) ⇒ Object



94
95
96
97
98
99
# File 'lib/isodoc/gem_tasks.rb', line 94

def uncomment_out_liquid(text)
  text
    .gsub("/* LIQUID_COMMENT", "")
    .gsub("LIQUID_COMMENT */", "")
    .gsub('"{{', "{{").gsub('}}"', "}}")
end