Sha256: 4e4bd79c26970fd9def0b7884dda82c4f00a8d0050c192104055d5b22a77a1db

Contents?: true

Size: 1.39 KB

Versions: 10

Compression:

Stored size: 1.39 KB

Contents

#!/usr/bin/env ruby

require 'thor'

module Writefully
  class Write < Thor
    desc "new", "creates a new article in the current directory"

    def new type, slug
      create_content_with type, slug
    end

    no_tasks do 
      def create_content_with type, slug
        new_position = get_last_post(type).split(/\//).last.match(/\A\d*/).to_s.to_i + 1
        dir_name = File.join(content_type(type), [new_position, slug].join('-'))
        Dir.mkdir dir_name
        add_assets dir_name
        add_readme_md dir_name
        add_meta dir_name
      end

      def add_readme_md dir_name
        file = File.new File.join(dir_name, 'README.md'), "w"
        file.puts("Replace with your content")
        file.close
      end

      def add_assets dir_name
        assets_dir_name = File.join(dir_name, 'assets')
        Dir.mkdir assets_dir_name
      end

      def add_meta dir_name
        meta = File.open(File.dirname(__FILE__) + "/../lib/sample/meta.yml").read
        file = File.new File.join(dir_name, 'meta.yml'), "w"
        file.puts meta
        file.close
      end

      def content_type type
        Dir.glob("*").select { |dir| dir.match(::Regexp.new(type)) }.first
      end

      def get_last_post type
        Dir.glob("#{content_type(type)}/*").sort_by do |item| 
          item.split(/\//).last.match(/\A\d*/).to_s.to_i
        end.last
      end
    end
  end
end

Writefully::Write.start(ARGV)

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
writefully-0.8.2 bin/wf-write
writefully-0.8.1 bin/wf-write
writefully-0.8.0 bin/wf-write
writefully-0.7.1 bin/wf-write
writefully-0.6.12 bin/wf-write
writefully-0.6.11 bin/wf-write
writefully-0.6.10 bin/wf-write
writefully-0.6.9 bin/wf-write
writefully-0.6.7 bin/wf-write
writefully-0.6.6 bin/wf-write