Class: Musedown::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/musedown.rb

Instance Method Summary collapse

Instance Method Details

#build(file_name) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/musedown.rb', line 41

def build(file_name)
    output_file = file_name
    if options[:output]
        puts("✏️  Output file will be #{options[:output]}")
        output_file = options[:output]
    end

    puts("👷‍♂️ Building file #{file_name}...")
    contents = nil
    File.open(file_name) do |file|
        contents = file.read
    end
    scores = []

    prebuilt_expression = /!\[.*\]\((?<file_name>.*\-mscz-1\.png)\)/
    prebuilt_matches = contents.scan(prebuilt_expression)

    puts("🔍 Found #{prebuilt_matches.length} previously built images...")
    prebuilt_matches.each do |match|
        score = Score.new(true)
        score.relative_image_file = match[0]
        resolved_image_file = File.join(File.dirname(file_name), score.relative_image_file)
        score.score_file = resolved_image_file.gsub("-mscz-1.png", ".mscz")
        scores << score
    end

    expression = /!\[.*\]\((?<file_name>.*\.mscz)\)/
    matches = contents.scan(expression)

    puts("🔍 Found unbuilt #{matches.length} score files...")
    matches.each do |match|
        score = Score.new(false)
        score.relative_score_file = match[0]
        score.score_file = File.join(File.dirname(file_name), score.relative_score_file)
        scores << score
    end

    scores.each do |score|
        sucess = score.build(options[:command])
        if !score.prebuilt and sucess
            contents = contents.gsub(score.relative_score_file, score.relative_image_file)
        end
    end

    puts("💾 Saving...")
    File.open(output_file, "w") do |file|
        file.write(contents)
    end

    puts "✅ Done!"
end