class Musedown::CLI

Public Instance Methods

build(file_name) click to toggle source
# 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