From dae09d983371d5858e028b37afd7d2b3877b776d Mon Sep 17 00:00:00 2001 From: Masato Ikeda Date: Thu, 16 May 2013 02:16:38 +0900 Subject: [PATCH] Add LTSV scanner LTSV is a text format described at: http://ltsv.org/ --- lib/coderay/helpers/file_type.rb | 1 + lib/coderay/scanners/ltsv.rb | 49 ++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 lib/coderay/scanners/ltsv.rb diff --git a/lib/coderay/helpers/file_type.rb b/lib/coderay/helpers/file_type.rb index f52f17e3..c47ae551 100644 --- a/lib/coderay/helpers/file_type.rb +++ b/lib/coderay/helpers/file_type.rb @@ -96,6 +96,7 @@ def shebang filename 'java' => :java, 'js' => :java_script, 'json' => :json, + 'ltsv' => :ltsv, 'mab' => :ruby, 'pas' => :delphi, 'patch' => :diff, diff --git a/lib/coderay/scanners/ltsv.rb b/lib/coderay/scanners/ltsv.rb new file mode 100644 index 00000000..91d8c345 --- /dev/null +++ b/lib/coderay/scanners/ltsv.rb @@ -0,0 +1,49 @@ +module CodeRay +module Scanners + + # A scanner for LTSV. + # http://ltsv.org + class Ltsv < Scanner + + register_for :ltsv + file_extension 'ltsv' + + protected + + def scan_tokens encoder, options + + state = :initial + + until eos? + + if match = scan(/(?:\r?\n)+/) + encoder.text_token match, :space + state = :initial if match.index(/\r?\n/) + + elsif (state == :initial || state == :tab) && match = scan(/[^:]+/) + encoder.text_token match, :key + state = :label + + elsif state == :label && match = scan(/:/) + encoder.text_token match, :delimiter + state = :colon + + elsif state == :colon && match = scan(/[^\t\r\n]*/) + encoder.text_token match, :value + state = :value + + elsif state == :value && match = scan(/\t/) + encoder.text_token match, :space + state = :tab + + else + raise + end + end + + encoder + end + end + +end +end