Sha256: 966d125b6514fe3f1d926c3560f2121d5c955e5a4c5718c6a852795b516a67de

Contents?: true

Size: 1.01 KB

Versions: 11

Compression:

Stored size: 1.01 KB

Contents

module Softcover
  module Commands
    module Exercises
      extend self

      def add_to_chapters!
        require 'securerandom'

        Dir["chapters/*.tex"].each do |path|

        	str = ""

        	in_exercise = false
        	n = 0
        	line_number = 0

        	lines = []
        	File.read(path).each_line { |line| lines.push line }

        	lines.each do |line|
        		str += line

        		case line
        		when %r{\\subsection{Exercises}}
        			in_exercise = true
        		when %r{\\end{enumerate}}
        			in_exercise = false
        		when %r{\\item}
        			if in_exercise && !(lines[line_number + 1] =~ /^%= <span/)
        				str += "%= <span class='exercise' id='ex-#{SecureRandom.hex(3)}'></span>\n"
        				n += 1
        			end
        		end

        		line_number += 1
        	end

        	File.open(path, "w") { |f| f.write str }

            exercises = n == 1 ? "exercise" : "exercises"
        	puts "#{path}: wrote #{n} #{exercises}"
        end
      end

    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
softcover-1.10.4 lib/softcover/commands/exercises.rb
softcover-1.10.3 lib/softcover/commands/exercises.rb
softcover-1.10.2 lib/softcover/commands/exercises.rb
softcover-1.10.1 lib/softcover/commands/exercises.rb
softcover-1.10.0 lib/softcover/commands/exercises.rb
softcover-1.9.1 lib/softcover/commands/exercises.rb
softcover-1.9.0 lib/softcover/commands/exercises.rb
softcover-1.8.3 lib/softcover/commands/exercises.rb
softcover-1.8.2 lib/softcover/commands/exercises.rb
softcover-1.8.1 lib/softcover/commands/exercises.rb
softcover-1.8.0 lib/softcover/commands/exercises.rb