Sha256: d89c90ad3bcfc1351f0adbbcb084a9e6a7b617bee95f6a221e0a59fb67b070a3

Contents?: true

Size: 1.72 KB

Versions: 9

Compression:

Stored size: 1.72 KB

Contents

#!/usr/bin/env ruby

require 'rubygems'
require 'redcloth'
require 'syntax/convertors/html'
require 'erb'

class Fixnum
  def ordinal
    # teens
    return 'th' if (10..19).include?(self % 100)
    # others
    case self % 10
    when 1: return 'st'
    when 2: return 'nd'
    when 3: return 'rd'
    else    return 'th'
    end
  end
end

class Time
  def pretty
    return "#{mday}#{mday.ordinal} #{strftime('%B')} #{year}"
  end
end

module Txt2Html
  def convert_syntax(syntax, source)
    return Syntax::Convertors::HTML.for_syntax(syntax).convert(source).gsub(%r!^<pre>|</pre>$!,'')
  end

  require File.dirname(__FILE__) + '/../lib/newgem/version.rb'

  version  = Newgem::VERSION::STRING
  download = ENV['HOMEPAGE']

  if ARGV.length >= 1
    src, template = ARGV
    template ||= File.dirname(__FILE__) + '/../website/template.html.erb'

  else
    puts("Usage: #{File.split($0).last} source.txt [template.html.erb] > output.html")
    exit!
  end

  template = ERB.new(File.open(template).read)

  title = nil
  body = nil
  File.open(src) do |fsrc|
    title_text = fsrc.readline
    body_text = fsrc.read
    syntax_items = []
    body_text.gsub!(%r!<(pre|code)[^>]*?syntax=['"]([^'"]+)[^>]*>(.*?)</\1>!m){
      ident = syntax_items.length
      element, syntax, source = $1, $2, $3
      syntax_items << "<#{element} class=\"syntax\">#{convert_syntax(syntax, source)}</#{element}>"
      "syntax-temp-#{ident}"
    }
    title = RedCloth.new(title_text).to_html.gsub(%r!<.*?>!,'').strip
    body = RedCloth.new(body_text).to_html
    body.gsub!(%r!(?:<pre><code>)?syntax-temp-(\d+)(?:</code></pre>)?!){ syntax_items[$1.to_i] }
  end
  stat = File.stat(src)
  created = stat.ctime
  modified = stat.mtime

  $stdout << template.result(binding)
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
newgem-0.25.0 script/txt2html.rb
newgem-0.22.2 script/txt2html.rb
newgem-0.23.0 script/txt2html.rb
newgem-0.23.1 script/txt2html.rb
newgem-0.24.0 script/txt2html.rb
newgem-0.22.1 script/txt2html.rb
newgem-0.27.0 script/txt2html.rb
newgem-0.29.0 script/txt2html.rb
newgem-0.28.0 script/txt2html.rb