Commit e4845522 authored by f's avatar f
Browse files

create posts

parent 1710706f
# frozen_string_literal: true
require 'thor'
require 'date'
require_relative 'commands/layout'
require_relative 'commands/field'
......@@ -21,6 +22,45 @@ module Sutty
end
map %w(--version -v) => :version
desc 'post NAME', 'Adds a post'
method_option :help, aliases: '-h', type: :boolean,
desc: 'Display usage information'
method_option :layout,
aliases: '-l',
type: :string,
desc: 'Layout',
required: true,
enum: Sutty::Cli::Commands::Layout.layouts
method_option :title,
aliases: '-t',
type: :string,
desc: 'Title',
required: true
method_option :date,
aliases: '-d',
type: :string,
desc: 'Date',
default: Date.today.to_s
# TODO: Bring locales from Jekyll configuration
method_option :locale,
aliases: '-L',
type: :string,
desc: 'Locale collection',
default: 'posts'
def post
if options[:help]
invoke :help, ['post']
else
require_relative 'commands/post'
Sutty::Cli::Commands::Post.new(options).execute
end
end
desc 'container NAME', 'Adds a container'
method_option :help, aliases: '-h', type: :boolean,
desc: 'Display usage information'
......
......@@ -2,6 +2,7 @@
require_relative '../command'
require 'tty-file'
require 'yaml'
module Sutty
module Cli
......@@ -15,7 +16,7 @@ module Sutty
end
def execute(input: $stdin, output: $stdout)
TTY::File.create_file data_layout, "---\n"
TTY::File.create_file data_layout, YAML.dump(data_default_fields)
TTY::File.create_file html_layout, "---\nlayout: default\n---\n\n"
end
......@@ -27,6 +28,22 @@ module Sutty
private
def data_default_fields
@data_default_fields ||= {
'title' => {
'type' => 'string',
'label' => {
'es' => 'Título',
'en' => 'Title'
},
'help' => {
'es' => '',
'en' => ''
}
}
}
end
def data_layout
@data_layout ||= File.join('_data', 'layouts', name + '.yml')
end
......
# frozen_string_literal: true
require_relative '../command'
require 'tty-logger'
require 'tty-file'
require 'yaml'
require 'securerandom'
require 'jekyll/utils'
module Sutty
module Cli
module Commands
class Post < Sutty::Cli::Command
attr_reader :options
CONTENT_FIELDS = %w[content markdown_content].freeze
def initialize(options)
@options = options
end
def execute(input: $stdin, output: $stdout)
if File.exist? path
logger.info "The file #{path} already exists"
return true
end
TTY::File.create_file path, YAML.dump(data) + "---\n\n"
end
private
def data
return @data if @data
@data = Hash[(data_layout.keys - CONTENT_FIELDS).map { |k| [k, nil] }]
@data['layout'] = options[:layout]
@data['uuid'] = SecureRandom.uuid
@data['liquid'] = false
@data
end
def data_layout
@data_layout ||= YAML.safe_load(File.read(File.join('_data', 'layouts', options[:layout] + '.yml')))
end
def slug
@slug ||= Jekyll::Utils.slugify(options[:title])
end
def dir
@dir ||= '_' + options[:locale]
end
def path
@path ||= File.join(dir, options[:date] + '-' + slug + '.markdown')
end
def logger
@logger ||= TTY::Logger.new
end
end
end
end
end
......@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
spec.summary = %q{Sutty CLI Tools}
spec.description = %q{Tools to develop Sutty's themes and plugins}
spec.homepage = "https://0xacab.org/sutty/#{spec.name}"
spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
spec.required_ruby_version = Gem::Requirement.new(">= 2.6.0")
spec.metadata = {
'bug_tracker_uri' => "#{spec.homepage}/issues",
......@@ -34,8 +34,10 @@ Gem::Specification.new do |spec|
'--quiet'
]
spec.required_ruby_version = '~> 2.6'
spec.add_dependency 'tty-command', '~> 0'
spec.add_dependency 'tty-file', '~> 0'
spec.add_dependency 'tty-logger', '~> 0'
spec.add_dependency 'jekyll', '~> 4'
spec.add_development_dependency 'tty', '~> 0'
spec.add_development_dependency 'pry', '~> 0'
end
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment