Sha256: 644d5865fe3b07a8179dd5a0f02d8f2f2725a67da738568054794aac8e50b84b

Contents?: true

Size: 1.06 KB

Versions: 1

Compression:

Stored size: 1.06 KB

Contents

require "obst/group_by_days"

module Obst
  class Notes
    include Enumerable

    def initialize(**opts)
      location = opts[:C] || '.'

      @notes = Enumerator.new do |enum|
        note_klass = Struct.new(:name, :dates, :tags, :refs)
        files = Dir.glob(File.join(location, '*.md')).map{|n| File.basename(n)}.to_set
        file_dates = Hash.new{ |h, k| h[k] = [] }

        GroupByDays.new(**opts).each do |log|
          log.file_changes.each do |name, op|
            file_dates[name] << log.time if files.include?(name)
          end
        end

        file_dates.each_pair do |file, dates|
          lines = File.foreach(File.join(location, file))
          tags = lines.first&.scan(/#(\w+)/)&.flatten || []
          refs = lines.each_with_object([]) do |line, arr|
            line.scan(/\[\[(.+?)\]\]/).flatten.each do |n|
              arr << n if files.include?("#{n}.md")
            end
          end
          enum.yield(note_klass.new(file, dates, tags, refs))
        end
      end
    end

    def each(&block)
      @notes.each(&block)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
obst-0.1.10 lib/obst/notes.rb