Sha256: 74bc03bca7a9926c297581e7bd5641c93a0327ad6a2a9ef3ff5bdc526f6c775f

Contents?: true

Size: 1.32 KB

Versions: 8

Compression:

Stored size: 1.32 KB

Contents

require 'markdo/command'
require 'uri'

module Markdo
  class RssCommand < Command
    def run
      uri_parser = URI::Parser.new

      items = Dir.
        glob(markdown_glob).
        map { |path| File.readlines(path, encoding: 'UTF-8') }.
        flatten.
        grep(%r(https?://)).
        map { |line| Item.new(title: clean(line), links: uri_parser.extract(line)) }

      xml = template(items)

      @stdout.puts(xml)
    end

    protected

    class Item
      attr_reader :title, :links

      def initialize(kwargs)
        @title = kwargs[:title]
        @links = kwargs[:links]
      end

      def link
        links && !links.empty? && links[0]
      end
    end

    def markdown_glob
      "#{@env['MARKDO_ROOT']}/*.md"
    end

    def clean(line)
      line.sub(/\s*[-*] \[.\]\s+/, '').chomp
    end

    def template(items)
      buf = []

      # No beginning of line whitespace allowed
      buf << %(<?xml version="1.0" encoding="UTF-8"?>)

      buf << %(<rss version="2.0">)
      buf << %(<channel>)
      buf << %(<title>Links in Markdo</title>)

      items.each do |item|
        buf << %(<item>)
        buf << %(<title>#{item.title}</title>)
        buf << %(<link>#{item.link}</link>)
        buf << %(</item>)
      end

      buf << %(</channel>)
      buf << %(</rss>)

      buf.join("\n")
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
markdo-0.1.12.alpha lib/markdo/rss_command.rb
markdo-0.1.11 lib/markdo/rss_command.rb
markdo-0.1.10 lib/markdo/rss_command.rb
markdo-0.1.9 lib/markdo/rss_command.rb
markdo-0.1.8 lib/markdo/rss_command.rb
markdo-0.1.7 lib/markdo/rss_command.rb
markdo-0.1.6 lib/markdo/rss_command.rb
markdo-0.1.5 lib/markdo/rss_command.rb