2018-01-29 22:19:10 +00:00
|
|
|
# Un sitio es un directorio dentro del directorio de sitios de cada
|
|
|
|
# Usuaria
|
|
|
|
class Site
|
|
|
|
|
|
|
|
attr_accessor :jekyll
|
2018-02-02 22:20:31 +00:00
|
|
|
attr_reader :path
|
2018-01-29 22:19:10 +00:00
|
|
|
|
2018-02-02 22:20:31 +00:00
|
|
|
def initialize(jekyll:, path: nil)
|
2018-01-29 22:19:10 +00:00
|
|
|
@jekyll = jekyll
|
2018-02-02 22:20:31 +00:00
|
|
|
@path = path || @jekyll.config['source']
|
2018-01-29 22:19:10 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Obtener el nombre del sitio
|
|
|
|
def name
|
|
|
|
@name ||= @jekyll.config['source'].split('/').last
|
|
|
|
end
|
|
|
|
|
|
|
|
# El id es el sitio sin puntos, para no confundir al routeador de
|
|
|
|
# rails
|
|
|
|
def id
|
|
|
|
@id ||= name.tr('.', '-')
|
|
|
|
end
|
|
|
|
alias :to_s :id
|
|
|
|
|
2018-02-19 19:33:28 +00:00
|
|
|
def read
|
|
|
|
@jekyll.read
|
|
|
|
end
|
|
|
|
|
|
|
|
# Fuerza relectura del sitio, eliminando el sitio actual en favor de
|
|
|
|
# un sitio nuevo, leído desde cero
|
|
|
|
def read!
|
|
|
|
@jekyll = Site.load_jekyll(@jekyll.path)
|
|
|
|
|
|
|
|
@jekyll.read
|
|
|
|
end
|
|
|
|
|
2018-02-09 21:28:27 +00:00
|
|
|
def data
|
|
|
|
if @jekyll.data.empty?
|
2018-02-19 19:33:28 +00:00
|
|
|
read
|
2018-02-09 21:28:27 +00:00
|
|
|
Rails.logger.info 'Leyendo data'
|
|
|
|
end
|
|
|
|
|
|
|
|
@jekyll.data
|
|
|
|
end
|
|
|
|
|
|
|
|
def config
|
|
|
|
if @jekyll.config.empty?
|
2018-02-19 19:33:28 +00:00
|
|
|
read
|
2018-02-09 21:28:27 +00:00
|
|
|
Rails.logger.info 'Leyendo config'
|
|
|
|
end
|
|
|
|
|
|
|
|
@jekyll.config
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2018-01-29 22:19:10 +00:00
|
|
|
# Los posts de este sitio
|
|
|
|
def posts
|
|
|
|
return @posts if @posts
|
|
|
|
|
2018-02-03 22:37:09 +00:00
|
|
|
Rails.logger.info 'Procesando posts'
|
|
|
|
|
2018-02-02 22:20:31 +00:00
|
|
|
if @jekyll.posts.docs.empty?
|
|
|
|
@jekyll.read
|
|
|
|
# Queremos saber cuantas veces releemos los articulos
|
|
|
|
Rails.logger.info 'Leyendo articulos'
|
|
|
|
end
|
2018-01-29 22:19:10 +00:00
|
|
|
|
|
|
|
# Los convertimos a una clase intermedia capaz de acceder a sus
|
|
|
|
# datos y modificarlos
|
|
|
|
@posts = @jekyll.posts.docs.map do |post|
|
2018-02-02 22:20:31 +00:00
|
|
|
Post.new(site: self, post: post)
|
2018-01-29 22:19:10 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-02-03 23:41:02 +00:00
|
|
|
def categories
|
2018-02-08 14:05:05 +00:00
|
|
|
everything_of :categories
|
2018-02-03 23:41:02 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def tags
|
2018-02-08 14:05:05 +00:00
|
|
|
everything_of :tags
|
|
|
|
end
|
|
|
|
|
|
|
|
def everything_of(attr)
|
|
|
|
posts.map do |p|
|
|
|
|
p.get_front_matter attr
|
|
|
|
end.flatten.uniq
|
2018-02-03 23:41:02 +00:00
|
|
|
end
|
|
|
|
|
2018-01-29 22:19:10 +00:00
|
|
|
# El directorio donde se almacenan los sitios
|
|
|
|
def self.site_path
|
|
|
|
File.join(Rails.root, '_sites')
|
|
|
|
end
|
|
|
|
|
|
|
|
# El directorio de los sitios de una usuaria
|
|
|
|
#
|
|
|
|
# Los sitios se organizan por usuaria, entonces los sitios que
|
|
|
|
# administra pueden encontrarse directamente en su directorio.
|
|
|
|
#
|
|
|
|
# Si comparten gestión con otras usuarias, se hacen links simbólicos
|
|
|
|
# entre sí.
|
|
|
|
def self.site_path_for(usuaria)
|
|
|
|
File.join(Site.site_path, usuaria.username)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Comprueba que el directorio parezca ser de jekyll
|
|
|
|
def self.jekyll?(dir)
|
|
|
|
File.directory?(dir) && File.exist?(File.join(dir, '_config.yml'))
|
|
|
|
end
|
|
|
|
|
2018-02-19 19:33:28 +00:00
|
|
|
def self.load_jekyll(path)
|
|
|
|
config = ::Jekyll.configuration('source' => path)
|
|
|
|
|
|
|
|
# No necesitamos cargar plugins en este momento
|
|
|
|
%w[plugins gems].each do |unneeded|
|
|
|
|
config[unneeded] = [] if config.key? unneeded
|
|
|
|
end
|
|
|
|
|
|
|
|
Jekyll::Site.new(config)
|
|
|
|
end
|
|
|
|
|
2018-01-29 22:19:10 +00:00
|
|
|
# Obtener todos los directorios de sitios asociados a esta usuaria
|
|
|
|
def self.all_for(usuaria)
|
|
|
|
@sites ||= Pathname.new(Site.site_path_for(usuaria))
|
|
|
|
.children.map(&:expand_path).map(&:to_s).map do |j|
|
|
|
|
|
|
|
|
next unless Site.jekyll? j
|
|
|
|
|
|
|
|
Dir.chdir(j) do
|
2018-02-19 19:33:28 +00:00
|
|
|
jekyll = Site.load_jekyll(Dir.pwd)
|
|
|
|
Site.new(jekyll: jekyll, path: j)
|
2018-01-29 22:19:10 +00:00
|
|
|
end
|
|
|
|
end.compact
|
|
|
|
end
|
|
|
|
end
|