Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 2.78 KB | None | 0 0
  1. # -*- encoding: utf-8 -*-
  2. require 'tempfile'
  3.  
  4. module Webgen::Tag
  5.  
  6.   # Provides syntax highlighting via the Pygments command-line tool.
  7.   class Pygments
  8.  
  9.     include Webgen::Tag::Base
  10.     include Webgen::WebsiteAccess
  11.  
  12.     # Highlight the body of the block.
  13.     def call(tag, body, context)
  14.  
  15.       error_file = Tempfile.new('webgen-pygments')
  16.       error_file.close
  17.  
  18.       `support/colorize.py 2>&1`
  19.       if $?.exitstatus != 0
  20.         raise Webgen::CommandNotFoundError.new('support/colorize.py', self.class.name, context.dest_node.alcn)
  21.       end
  22.  
  23.       if param('tag.pygments.process_body')
  24.         body = website.blackboard.invoke(:content_processor, 'tags').call(context.clone(:content => body)).content
  25.       end
  26.       cmd = "support/colorize.py #{param('tag.pygments.lang')} #{param('tag.pygments.tab_width')} 2>'#{error_file.path}'"
  27.       result = IO.popen(cmd, 'r+') do |io|
  28.         io.write(body)
  29.         io.close_write
  30.         io.read
  31.       end
  32.       if $?.exitstatus != 0
  33.         File.foreach(error_file.path).each do |line|
  34.           log(:warn) { "pygmentize> #{line}" }
  35.         end
  36.       end
  37.         result
  38.     end
  39.  
  40.   end
  41.  
  42.   class PygmentsFile
  43.  
  44.     include Webgen::Tag::Base
  45.     include Webgen::WebsiteAccess
  46.  
  47.     # Highlight the body of the block.
  48.     def call(tag, body, context)
  49.  
  50.       error_file = Tempfile.new('webgen-pygments-file')
  51.       error_file.close
  52.  
  53.       `support/colorize.py 2>&1`
  54.       if $?.exitstatus != 0
  55.         raise Webgen::CommandNotFoundError.new('support/colorize.py', self.class.name, context.dest_node.alcn)
  56.       end
  57.  
  58.         body = File.read(param('tag.pygmentsfile.filename'))
  59.       cmd = "support/colorize.py #{param('tag.pygmentsfile.lang')} #{param('tag.pygmentsfile.tab_width')} 2>'#{error_file.path}'"
  60.       result = IO.popen(cmd, 'r+') do |io|
  61.         io.write(body)
  62.         io.close_write
  63.         io.read
  64.       end
  65.       if $?.exitstatus != 0
  66.         File.foreach(error_file.path).each do |line|
  67.           log(:warn) { "pygmentize> #{line}" }
  68.         end
  69.       end
  70.         result
  71.     end
  72.  
  73.   end
  74.  
  75. end
  76.  
  77. config = Webgen::WebsiteAccess.website.config
  78. config['contentprocessor.tags.map']['pygments'] = 'Webgen::Tag::Pygments'
  79. config['contentprocessor.tags.map']['pygmentsfile'] = 'Webgen::Tag::PygmentsFile'
  80.  
  81. config.tag.pygments.lang('text', :doc => 'The language', :mandatory => 'default')
  82. config.tag.pygments.tab_width(8, :doc => 'Number of spaces used for a tabulator')
  83. config.tag.pygments.process_body(false, :doc => 'The tag body will be scanned for tags first if true')
  84.  
  85. config.tag.pygmentsfile.lang('text', :doc => 'The language', :mandatory => 'default')
  86. config.tag.pygmentsfile.tab_width(8, :doc => 'Number of spaces used for a tabulator')
  87. config.tag.pygmentsfile.filename(nil, :doc => 'The source file', :mandatory => true)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement