Class: Octopress::Multilingual::Translate

Inherits:
Command
  • Object
show all
Defined in:
lib/octopress-multilingual/command.rb

Class Method Summary collapse

Class Method Details

.generate_id(paths) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/octopress-multilingual/command.rb', line 22

def self.generate_id(paths)
  id = Digest::MD5.hexdigest(paths.join)
  translated = []
  paths.each do |path|
    if File.file? path
      contents = File.read(path)
      contents.sub!(/\A(---\s+.+?\s+)---/m) do
        fm = $1.sub(/translation_id:.+\n?/,'')
        fm << "translation_id: #{id}\n"
        fm << "---"
      end

      File.open(path, 'w+') {|f| f.write(contents) }

      translated << path
    end
  end

  puts "translation_id: #{id}"
  puts "Added to:"
  puts translated.map {|p| "  - #{p}" }.join("\n")
end

.init_with_program(p) ⇒ Object



11
12
13
14
15
16
17
18
19
20
# File 'lib/octopress-multilingual/command.rb', line 11

def self.init_with_program(p)
  p.command(:id) do |c|
    c.syntax 'id <path> [path path...]>'
    c.description "Generate a uniqe id to link translated posts or pages."

    c.action do |args|
      generate_id(args)
    end
  end
end