枕を欹てて聴く

香炉峰の雪は簾を撥げて看る

LDRize Siteinfo parse

多少修正
LDRize作者, snj14さんの許可を受け, Wedata上にLDRizeデータを放り込みました.
以下のRubyのscriptによって作成されたデータを一括登録しています

本家のものにあわせて.
はじめに書いたRubyのほう.

require 'open-uri'
require 'kconv'
require 'json'
require 'time'
$KCODE = 'u'

@@keys = %w( name paragraph domain link name stripe focus height view disable)
def parse text
  reg = /^ ([^:\s]+)\s*: *'?((?:'|[^'])*?)'? *,? *$/u
  reg_title1 = /^-\s*\[\[([^:]+):([^\]]+)\]\]/u
  reg_title2 = /^-\s*(.+)/u
  info = create_info
  dealing = false
  count = 0
  memo = text.split(/\r\n|\r|\n/u).inject([]){|memo, line|
    if line[0..1] === '//'# comment
      puts 'comment'
    elsif !(line[0] === 32)
      unless dealing
        if line =~ reg_title1
          count += 1
          #puts line
          dealing = true
          info['name'] = $1
          info['data']['exampleUrl'] = $2
          #puts line
        elsif line =~ reg_title2
          count += 1
          #puts line
          dealing = true
          info['name'] = $1
          #puts line
        else
          #puts line
        end
      else
        dealing = false
        memo << info
        info = create_info
      end
    elsif reg =~ line
      if dealing
        val = $2
        key = $1
        if @@keys.include?(key)
          info['data'][key] = val.downcase === 'true'  ? true   :
                             val.downcase === 'false' ? false  : val.gsub(/\\\\/u, '\\')
        else
          puts key
        end
      else
        puts line
      end
    else
      puts line
    end

    memo
  }
  memo << info# 最後のひとつ
  puts memo.size
  memo
end

@@now = Time.now.iso8601
@@url = 'http://white.s151.xrea.com/wiki/index.php?cmd=ldrize'
def create_info
  {
    'name' => 'none',
    'data' => {},
  }
end

def generate
  JSON.pretty_generate(parse(open("http://white.s151.xrea.com/wiki/index.php?cmd=ldrize").read.toutf8))
end

File.open(File.expand_path('~/dev/ldrize/wedata.json'), 'wb') do |f|
  f << generate
end

で, AppJetで使おうと思って, JSにportingしたもの.

function parse(text){
  var reg = /^ ([^:\s]+)\s*: *'?((?:'|[^'])*?)'? *,? *$/
  var reg_title1 = /^-\s*\[\[([^:]+):([^\]]+)\]\]/
  var reg_title2 = /^-\s*(.+)/
  var info = creat_info();
  var dealing = false;
  var memo = [];
  text.split(/\r\n|\r|\n/).forEach(function(line){
    if(line.indexOf('//') === 0){
      //comment
    } else if(!(line.indexOf(' ') === 0)){
      if(!dealing){
        if(reg_title1.test(line)){
          dealing = true;
          info['name'] = RegExp.$1;
          info['data']['exampleUrl'] = RegExp.$2;
        } else if(reg_title2.test(line)){
          dealing = true;
          info['name'] = RegExp.$1;
        }
      } else {
        dealing = false;
        memo.push(info);
        info = create_info();
      }
    } else if(reg.test(line)){
      if(dealing){
        var key = RegExp.$1;
        var val = RegExp.$2;
        info['data'][key] = val.toLowerCase() === 'true'  ? true   :
                            val.toLowerCase() === 'false' ? false  : val.replace(/\\\\/g, '\\');
      }
    }
  });
  memo.push(info);
  return memo;
}

var create_info = (function(){
  var url = 'http://white.s151.xrea.com/wiki/index.php?cmd=ldrize';
  var time = (function(){
    var trim = function(num, l){
      if(num<0) num = -(num);
      if(typeof(num) != 'string') num = ''+num;
      var s = l - num.length;
      while(s>0){
        num = '0'+num;
        --s;
      }
      return num;
    }
    var d = new Date;
    var YYYY   = trim(d.getFullYear(), 4);
    var MM_1   = trim(d.getMonth()+1, 2);
    var DD     = trim(d.getDate(), 2);
    var T      = 'T';
    var HH     = trim(d.getHours(), 2);
    var MM_2   = trim(d.getMinutes(), 2);
    var SS     = trim(d.getSeconds(), 2);
    var offset = d.getTimezoneOffset();
    var plus   = '-';
    if(offset<=0){
      plus     = '+';
      offset   = -(offset);
    }
    var ZZ_1   = trim(offset/60, 2);
    var ZZ_2   = trim(offset%60, 2);

    return YYYY+'-'+MM_1+'-'+DD+T+HH+':'+MM_2+':'+SS+plus+ZZ_1+':'+ZZ_2;
  })();
  return function(){
    return {
      'name' : 'none',
      'database_resource_url' : url,
      'updated_at' : time,
      'created_at' : time,
      'resource_url' : url,
      'data' : {},
    }
  }
})();