Sha256: 59828b8408d99acf0cefc75fda4bee58694a8482ea189b3862f72653a88b42d0

Contents?: true

Size: 1.27 KB

Versions: 1

Compression:

Stored size: 1.27 KB

Contents

require 'markdo/command'
require 'uri'

module Markdo
  class RssCommand < Command
    def run
      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.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

1 entries across 1 versions & 1 rubygems

Version Path
markdo-0.1.4 lib/markdo/rss_command.rb