Support for dryml files to gettext:find rake task

29 views
Skip to first unread message

Antero Hynynen

unread,
Jan 7, 2015, 12:07:51 PM1/7/15
to hob...@googlegroups.com
Hi, we are using fast_gettext with hobo 2.0.0.pre7 as we like to use dryml templates. We added feeble support for dryml files to gettext:find rake task by creating a dryml parser to lib by modifying an erb parser a bit and then by calling it in gettext:find task. Here are related files if someone is interested..

lib/tasks/gettext.rake
namespace :gettext do

 task
:dryml_parser do
 
require 'dryml_parser'
 
end

 
def files_to_translate
 
Dir.glob("{app,lib,config,#{locale_path}}/**/*.{rb,erb,haml,slim,dryml}")
 
end
end

task
:'gettext:find' => :'gettext:dryml_parser'


lib/dryml_parser
require 'erb'
require 'gettext/tools/parser/ruby'
require 'gettext/tools/xgettext'

module GetText
 
class DrymlParser
 
@config = {
 
:extnames => ['.dryml']
 
}

 
class << self
 
# Sets some preferences to parse ERB files.
 
# * config: a Hash of the config. It can takes some values below:
 
def init(config)
 config
.each{|k, v|
 
@config[k] = v
 
}
 
end

 
def target?(file) # :nodoc:
 
@config[:extnames].each do |v|
 
return true if File.extname(file) == v
 
end
 
false
 
end

 
# Parses eRuby script located at `path`.
 
#
 
# This is a short cut method. It equals to `new(path,
 
# options).parse`.
 
#
 
# @return [Array<POEntry>] Extracted messages
 
# @see #initialize and #parse
 
def parse(path, options={})
 parser
= new(path, options)
 parser
.parse
 
end
 
end

 MAGIC_COMMENT
= /\A#coding:.*\n/

 
# @param path [String] eRuby script path to be parsed
 
# @param options [Hash]
 
def initialize(path, options={})
 
@path = path
 
@options = options
 
end

 
# Extracts messages from @path.
 
#
 
# @return [Array<POEntry>] Extracted messages
 
def parse
 content
= IO.read(@path)
 src
= ERB.new(content).src

 
# Force the src encoding back to the encoding in magic comment
 
# or original content.
 encoding
= detect_encoding(src) || content.encoding
 src
.force_encoding(encoding)

 
# Remove magic comment prepended by erb in Ruby 1.9.
 src
= src.gsub(MAGIC_COMMENT, "")

 
RubyParser.new(@path, @options).parse_source(src)
 
end

 
def detect_encoding(source)
 
if /\A#coding:(.*)\n/ =~ source
 $1
 
else
 
nil
 
end
 
end
 
end
end

if __FILE__ == $0
 
# ex) ruby glade.dryml?
 ARGV
.each do |file|
 p
GetText::DrymlParser.parse(file)
 
end
end

GetText::Tools::XGetText.add_parser(GetText::DrymlParser)

As this code is based on erb parser it does not support all dryml features. Any ideas to make this better would be welcome..

Thanks

Ignacio Huerta

unread,
Jan 12, 2015, 5:49:43 AM1/12/15
to hob...@googlegroups.com
Thanks for sharing Antero, I'm afraid you are the first one to try
adding gettext support to Dryml!
> deftarget?(file)# :nodoc:
> @config[:extnames].each do|v|
> returntrueifFile.extname(file)==v
> end
> false
> end
>
> # Parses eRuby script located at `path`.
> #
> # This is a short cut method. It equals to `new(path,
> # options).parse`.
> #
> # @return [Array<POEntry>] Extracted messages
> # @see #initialize and #parse
> defparse(path,options={})
> parser =new(path,options)
> parser.parse
> end
> end
>
> MAGIC_COMMENT =/\A#coding:.*\n/
>
> # @param path [String] eRuby script path to be parsed
> # @param options [Hash]
> definitialize(path,options={})
> @path=path
> @options=options
> end
>
> # Extracts messages from @path.
> #
> # @return [Array<POEntry>] Extracted messages
> defparse
> content =IO.read(@path)
> src =ERB.new(content).src
>
> # Force the src encoding back to the encoding in magic comment
> # or original content.
> encoding =detect_encoding(src)||content.encoding
> src.force_encoding(encoding)
>
> # Remove magic comment prepended by erb in Ruby 1.9.
> src =src.gsub(MAGIC_COMMENT,"")
>
> RubyParser.new(@path,@options).parse_source(src)
> end
>
> defdetect_encoding(source)
> if/\A#coding:(.*)\n/ =~ source
> $1
> else
> nil
> end
> end
> end
> end
>
> if__FILE__ ==$0
> # ex) ruby glade.dryml?
> ARGV.each do|file|
> p GetText::DrymlParser.parse(file)
> end
> end
>
> GetText::Tools::XGetText.add_parser(GetText::DrymlParser)
> |
>
> As this code is based on erb parser it does not support all dryml
> features. Any ideas to make this better would be welcome..
>
> Thanks
>
> --
> You received this message because you are subscribed to the Google
> Groups "Hobo Dev" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to hobodev+u...@googlegroups.com
> <mailto:hobodev+u...@googlegroups.com>.
> To post to this group, send email to hob...@googlegroups.com
> <mailto:hob...@googlegroups.com>.
> Visit this group at http://groups.google.com/group/hobodev.
> For more options, visit https://groups.google.com/d/optout.

Antero Hynynen

unread,
Jan 13, 2015, 10:55:16 AM1/13/15
to hob...@googlegroups.com
Here is updated version of lib/dryml_parser.rb which adds light support for string interpolation syntax i.e. #{...} which we also use for translation keys. Note that only basic usage is supported by this code..



     
# convert dryml scriptlets to erb form..
      content = content.gsub(/\#\{(.*?)\}/m) { "<%= #{$1} %>" }

Ignacio Huerta

unread,
Jan 14, 2015, 7:23:46 AM1/14/15
to hob...@googlegroups.com
Nice work, thanks!

El 13-01-2015 a las 16:55, Antero Hynynen escribió:
> Here is updated version of *lib/dryml_parser.rb* which adds light
Reply all
Reply to author
Forward
0 new messages