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
# File 'lib/isodoc/gem_tasks.rb', line 113

def compile_scss(filename)
  load_scss_paths(filename)
  Dir.mktmpdir do |dir|
    File.write(File.join(dir, "variables.scss"), fonts_placeholder)
    SassC.load_paths << dir
    sheet_content = File.read(filename, encoding: "UTF-8")
      .gsub(%r<([a-z])\.([0-9])(?=[^{}]*{)>m, "\\1.__WORD__\\2")
    SassC::Engine.new(%<@use "variables" as *;\n#{sheet_content}>,
                      syntax: :scss, 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_listObject



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

def self.css_list
  @@css_list
end

.fonts_placeholderObject



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

def fonts_placeholder
  "    $bodyfont: \"{{bodyfont}}\";\n    $headerfont: \"{{headerfont}}\";\n    $monospacefont: \"{{monospacefont}}\";\n    $normalfontsize: \"{{normalfontsize}}\";\n    $smallerfontsize: \"{{smallerfontsize}}\";\n    $footnotefontsize: \"{{footnotefontsize}}\";\n    $monospacefontsize: \"{{monospacefontsize}}\";\n  TEXT\nend\n"

.git_cache_compiled_filesObject



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_filesObject



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

.installObject



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



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

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)].each do |name|
     SassC.load_paths << name
   end
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