# -*- coding: utf-8 -*- require 'diglossa/remote' #require 'remote' module Diglossa class Parse attr_accessor :app_dir, :document, :root, :dbauthors, :author_nic, :nics, :locale, :rpp, :filepaths, :warns, :counter #, :config_path, :revision attr_reader :config def initialize(app_dir) #, config_path = nil) self.app_dir = File.expand_path(app_dir) + '/' @warns = {file:[], size:[]} @counter = [] #load_couchapprc end def parse_author(nic) # FIXME: поправить название, это чтение файла fn = File.expand_path(File.join(app_dir, nic)) fullname = "#{fn}.json" return nil unless File.exist?(fullname) doc = YAML.load_file fullname doc end def build root, push=false # FIXME: Lib to .diglossarc fn = File.expand_path(File.join(Diglossa::Lib, root)) fullname = File.exist?("#{fn}.json") ? "#{fn}.json" : File.exist?("#{fn}.yml") ? "#{fn}.yml" : false return nil unless fullname doc = fullname ? YAML.load_file(fullname) : "no .json or .yml file at #{root}" @nics = doc["authors"] remote = Diglossa::Remote.new(app_dir) #puts "locales #{remote.config["locales"]}" @rpp = {} @dbauthors = remote.author(doc["authors"]) @author_nic = root.split("/")[1].downcase #lang = root.split('/').first.downcase @locale = "ru" title = root.split("/").last # .gsub("_", " ") if push == "book" jsru = {isFolder:true, expand: true, children:[]} jsen = {isFolder:true, expand: true, children:[]} traverse jsru, doc["tree"], root, title do |path| parallels path # cause of rpp, alas end @locale = "en" traverse jsen, doc["tree"], root, title [{type:"book", url:root.to_url, jstree:jsru, rpp:rpp, locale:"ru"}, {type:"book", url:root.to_url, jstree:jsen, rpp:rpp, locale:"en"}] elsif push == "docs" jsru = {url:root.to_url, isFolder:true, expand: true, children:[]} traverse jsru, doc["tree"], root, title do |path| parallels path, true end elsif push == "warns" jsru = {url:root.to_url, isFolder:true, expand: true, children:[]} traverse jsru, doc["tree"], root, title do |path| parallels path end warns end end private def parallels path, push = false remote = Diglossa::Remote.new(app_dir) dpath = File.expand_path(File.join(Diglossa::Lib, path)) return if File.directory?(dpath) @counter = [] docs = [] nics.each do |nic| author = dbauthors.find{|a|a["nic"] == nic} fpath = "#{dpath}.#{nic}" unless File.exist?(fpath) warns[:file].push fpath next end docs += paragraphs(fpath, path.to_url, nic, author["lang"], push) end if push puts "pushing #{docs.size} - #{path}" remote.texts_push path.to_url, nics, docs end # some stupid heuristique for rpp vol = docs.inject(0){|vol,doc| vol + doc[:text].size} rows_per_page = vol == 0 ? 25 : (10000*(docs.size)/vol).round rpp[path.to_url] = rows_per_page #.to_s if (nics.size != counter.size) || (counter.uniq.size != 1) warns[:size].push "#{path} - #{counter.inspect}" end end def paragraphs fpath, url, nic, lang, push docs = [] lines = File.readlines fpath counter.push lines.size lines.each do |line| line.gsub!(/\s+/, " ").strip! #.gsub!("*", " *").gsub!(" /", "/") next if line.empty? docs << {type:'text', url:url, nic:nic, lang:lang, text:line} end docs.each_with_index.map{|doc, index| doc[:pos] = index; doc} end def parse_nics narrow = true, nics = nil nics ||= @nics authors = {} nics.each do |nic| next if narrow && nic == author_nic dbauthor = dbauthors.find{|a| a["nic"] == nic} hash = {:name=>dbauthor[locale], :lang=>dbauthor["lang"]} hash["author"] = true if nic == author_nic authors[nic] = hash end authors end def traverse(jstree, doc, path, title=fase, &blk) case doc when Hash hash = {} if doc["title"] == title hash = jstree @nics = doc["authors"] if doc["authors"] hash["nics"] = parse_nics false hash["title"] = doc[locale] else path += "/#{doc["title"].gsub(" ","_")}" title = doc[locale] @nics = doc["authors"] if doc["authors"] authors = parse_nics nics dir = doc["children"].nil? hash = dir ? {url: path.to_url, title: title, nics: authors} : {url: path.to_url, title: title, isFolder: true, expand: true, children:[]} jstree[:children].push hash unless doc["title"] == title end traverse(hash, doc["children"], path, title, &blk) when Array doc.each {|v| traverse(jstree, v, path, title, &blk) } else blk.call(path) if blk end end # def print_warns # unless warns_file.empty? # puts "NO FILE: " # тут непонятка - при отключении paragraphs выводятся warnings, что не нужно # pp warns_file # end # unless warns_size.empty? # puts "NON EQUAL SIZES: " # pp warns_size # end # end def json fname # это для блога # document = {} puts "building DOC fname: #{fname}" puts "APDIR.self #{self.app_dir}" puts "APDIR #{app_dir}" path = File.expand_path(File.join(app_dir, fname)) puts "path #{path}" uuid = UUIDTools::UUID.sha1_create UUIDTools::UUID_DNS_NAMESPACE, "#{fname}" puts "_ID = #{path} - #{uuid}" lines = File.readlines path lines.reject!{|c|c.empty? || c == "\n"} document[:_id] = uuid.to_s document[:title] = lines.shift.strip document[:tags] = lines.shift.strip.split(/[[:punct:]]| /).reject!{|c|c.empty?} #markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML, :autolink => true, :space_after_headers => true) #body = markdown.render lines.join("\n") document[:body] = "body" puts "----------" pp document document end end end