class JSONSerializer

Attributes

json[RW]
linkauthor[RW]

Public Class Methods

new(json, linkauthor) click to toggle source
# File lib/json-serializer.rb, line 7
def initialize(json, linkauthor)
  @json = json
  @linkauthor = linkauthor
end

Public Instance Methods

compile() click to toggle source
# File lib/json-serializer.rb, line 12
def compile
  structured_gists = Hash.new{ |h,k| h[k] = Hash.new(&h.default_proc) }

  entries = JSON.parse(json)
  entries.each do |arr|
    title = arr["Title"] != nil ? arr["Title"].strip() : nil
    authors = arr["Authors"] != nil ? arr["Authors"].strip() : nil
    description = arr["Description"] != nil ? arr["Description"].strip() : nil
    section = arr["Section"] != nil ? arr["Section"].strip() : nil
    subsection = arr["Subsection"] != nil ? arr["Subsection"].strip() : nil
    url = arr["URL"] != nil ? arr["URL"].strip() : nil
    url_user = arr["URL_USER"] != nil ? arr["URL_USER"].strip() : nil

    if title == nil
      puts "Error: A title must be included for #{url}"
      next
    end
    
    if section == nil || subsection == nil
      puts "Error: #{title} must have a section and subsection to be compiled!"
      next
    end

    structured_gists[section][subsection][title] = {"Authors"=>authors, "Description"=>description,
                                                    "URL"=>url, "URL_USER"=>url_user}
  end
  structured_gists = structured_gists.sort.to_h
  structured_gists.keys.each do |k|
    structured_gists[k] = structured_gists[k].sort.to_h
    structured_gists[k].keys.each do |l|
      structured_gists[k][l] = structured_gists[k][l].sort.to_h
    end
  end
  
  return structured_gists
end
to_html(file) click to toggle source
# File lib/json-serializer.rb, line 72
def to_html(file)
  structured_gists = compile()
  File.open(file,"w") do |f|
    f.puts("<!DOCTYPE HTML>")
    f.puts("<html>")
    f.puts("  <head>\n    <meta charset=\"UTF-8\">\n  </head>");
    f.puts("  <body>")
    f.puts("    Generated by <a href=\"https://github.com/claymcleod/gist-compile\">gist-compile</a> at "+Time.new.strftime("%Y-%m-%d %H:%M:%S"))
    structured_gists.each do |j, v|
      f.puts("    <div class=\"section\">\r")
      f.puts("    <a id=\"#{j}\"></a>")
      f.puts("    <h1>#{j}</h1>\r")
      f.puts("    </div>")
      structured_gists[j].each do |k, v|
        f.puts("      <h2>#{k}</h2>\r")
        f.puts("\r\n")
        structured_gists[j][k].each do |t, v|
          url= structured_gists[j][k][t]["URL"]
          if url == nil
            f.puts("        <h3>#{t}</h3>\r")
          else
            f.puts("        <h3><a href=\'#{url}\'>#{t}</a></h3>")
          end
          f.puts("          <ul>");
          structured_gists[j][k][t].each do |l, v|
            if l != "URL" && l != "URL_USER" && v != nil
              if linkauthor && l == "Authors"
                f.puts("          <li>#{l}: <a href=\"http://#{structured_gists[j][k][t]["URL_USER"]}\">#{v}</a></li>\r")
              else
                f.puts("          <li>#{l}: #{v}</li>\r")
              end
            end
          end
          f.puts("\r\n")
          f.puts("          </ul>\r")
        end
      end
    end
    f.puts("    </body>");
    f.puts("</html>");
    f.close()
  end
end
to_md(file) click to toggle source
# File lib/json-serializer.rb, line 49
def to_md(file)
  structured_gists = compile()
  File.open(file,"w") do |f|
    structured_gists.each do |j, v|
      f.puts("# #{j}\r")
      structured_gists[j].each do |k, v|
        f.puts("### #{k}\r")
        f.puts("\r\n")
        structured_gists[j][k].each do |t, v|
          f.puts("##### #{t}\r")
          structured_gists[j][k][t].each do |l, v|
            if v != nil
              f.puts("* #{l}: #{v}\r")
            end
          end
          f.puts("\r\n")
        end
      end
    end
    f.close()
  end
end