plugin.rb 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #require 'version.rb'
  2. module Jekyll
  3. class Site
  4. alias :process_org :process
  5. def process
  6. if !self.config['baseurl']
  7. self.config['baseurl'] = ""
  8. end
  9. #Variables
  10. config['baseurl_root'] = self.config['baseurl']
  11. baseurl_org = self.config['baseurl']
  12. languages = self.config['languages']
  13. dest_org = self.dest
  14. #Loop
  15. self.config['lang'] = languages.first
  16. puts
  17. puts "Building site for default language: \"#{self.config['lang']}\" to: " + self.dest
  18. process_org
  19. languages.drop(1).each do |lang|
  20. # Build site for language lang
  21. self.dest = self.dest + "/" + lang
  22. self.config['baseurl'] = self.config['baseurl'] + "/" + lang
  23. self.config['lang'] = lang
  24. puts "Building site for language: \"#{self.config['lang']}\" to: " + self.dest
  25. process_org
  26. #Reset variables for next language
  27. self.dest = dest_org
  28. self.config['baseurl'] = baseurl_org
  29. end
  30. puts 'Build complete'
  31. end
  32. alias :read_posts_org :read_posts
  33. def read_posts(dir)
  34. if dir == ''
  35. posts = read_things("_i18n/#{self.config['lang']}","_posts", Post)
  36. posts.each do |post|
  37. post.categories = []
  38. if post.date != ''
  39. if post.published && (self.future || post.date <= self.time)
  40. aggregate_post_info(post)
  41. end
  42. end
  43. end
  44. else
  45. read_posts_org(dir)
  46. end
  47. end
  48. end
  49. class LocalizeTag < Liquid::Tag
  50. def initialize(tag_name, key, tokens)
  51. super
  52. @key = key.strip
  53. end
  54. def render(context)
  55. if "#{context[@key]}" != "" #Check for page variable
  56. key = "#{context[@key]}"
  57. else
  58. key = @key
  59. end
  60. lang = context.registers[:site].config['lang']
  61. candidate = YAML.load_file(context.registers[:site].source + "/_i18n/#{lang}.yml")
  62. path = key.split(/\./) if key.is_a?(String)
  63. while !path.empty?
  64. key = path.shift
  65. if candidate[key]
  66. candidate = candidate[key]
  67. else
  68. candidate = ""
  69. end
  70. end
  71. if candidate == ""
  72. puts "Missing i18n key: " + lang + ":" + key
  73. "*" + lang + ":" + key + "*"
  74. else
  75. candidate
  76. end
  77. end
  78. end
  79. module Tags
  80. class LocalizeInclude < IncludeTag
  81. def render(context)
  82. if "#{context[@file]}" != "" #Check for page variable
  83. file = "#{context[@file]}"
  84. else
  85. file = @file
  86. end
  87. includes_dir = File.join(context.registers[:site].source, '_i18n/' + context.registers[:site].config['lang'])
  88. if File.symlink?(includes_dir)
  89. return "Includes directory '#{includes_dir}' cannot be a symlink"
  90. end
  91. if file !~ /^[a-zA-Z0-9_\/\.-]+$/ || file =~ /\.\// || file =~ /\/\./
  92. return "Include file '#{file}' contains invalid characters or sequences"
  93. end
  94. Dir.chdir(includes_dir) do
  95. choices = Dir['**/*'].reject { |x| File.symlink?(x) }
  96. if choices.include?(file)
  97. source = File.read(file)
  98. partial = Liquid::Template.parse(source)
  99. context.stack do
  100. context['include'] = parse_params(context) if @params
  101. contents = partial.render(context)
  102. site = context.registers[:site]
  103. ext = File.extname(file)
  104. converter = site.converters.find { |c| c.matches(ext) }
  105. contents = converter.convert(contents) unless converter.nil?
  106. contents
  107. end
  108. else
  109. "Included file '#{file}' not found in #{includes_dir} directory"
  110. end
  111. end
  112. end
  113. end
  114. end
  115. end
  116. Liquid::Template.register_tag('t', Jekyll::LocalizeTag)
  117. Liquid::Template.register_tag('translate', Jekyll::LocalizeTag)
  118. Liquid::Template.register_tag('tf', Jekyll::Tags::LocalizeInclude)
  119. Liquid::Template.register_tag('translate_file', Jekyll::Tags::LocalizeInclude)