Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- encoding: utf-8 -*-
- require 'tempfile'
- module Webgen::Tag
- # Provides syntax highlighting via the Pygments command-line tool.
- class Pygments
- include Webgen::Tag::Base
- include Webgen::WebsiteAccess
- # Highlight the body of the block.
- def call(tag, body, context)
- error_file = Tempfile.new('webgen-pygments')
- error_file.close
- `support/colorize.py 2>&1`
- if $?.exitstatus != 0
- raise Webgen::CommandNotFoundError.new('support/colorize.py', self.class.name, context.dest_node.alcn)
- end
- if param('tag.pygments.process_body')
- body = website.blackboard.invoke(:content_processor, 'tags').call(context.clone(:content => body)).content
- end
- cmd = "support/colorize.py #{param('tag.pygments.lang')} #{param('tag.pygments.tab_width')} 2>'#{error_file.path}'"
- result = IO.popen(cmd, 'r+') do |io|
- io.write(body)
- io.close_write
- io.read
- end
- if $?.exitstatus != 0
- File.foreach(error_file.path).each do |line|
- log(:warn) { "pygmentize> #{line}" }
- end
- end
- result
- end
- end
- class PygmentsFile
- include Webgen::Tag::Base
- include Webgen::WebsiteAccess
- # Highlight the body of the block.
- def call(tag, body, context)
- error_file = Tempfile.new('webgen-pygments-file')
- error_file.close
- `support/colorize.py 2>&1`
- if $?.exitstatus != 0
- raise Webgen::CommandNotFoundError.new('support/colorize.py', self.class.name, context.dest_node.alcn)
- end
- body = File.read(param('tag.pygmentsfile.filename'))
- cmd = "support/colorize.py #{param('tag.pygmentsfile.lang')} #{param('tag.pygmentsfile.tab_width')} 2>'#{error_file.path}'"
- result = IO.popen(cmd, 'r+') do |io|
- io.write(body)
- io.close_write
- io.read
- end
- if $?.exitstatus != 0
- File.foreach(error_file.path).each do |line|
- log(:warn) { "pygmentize> #{line}" }
- end
- end
- result
- end
- end
- end
- config = Webgen::WebsiteAccess.website.config
- config['contentprocessor.tags.map']['pygments'] = 'Webgen::Tag::Pygments'
- config['contentprocessor.tags.map']['pygmentsfile'] = 'Webgen::Tag::PygmentsFile'
- config.tag.pygments.lang('text', :doc => 'The language', :mandatory => 'default')
- config.tag.pygments.tab_width(8, :doc => 'Number of spaces used for a tabulator')
- config.tag.pygments.process_body(false, :doc => 'The tag body will be scanned for tags first if true')
- config.tag.pygmentsfile.lang('text', :doc => 'The language', :mandatory => 'default')
- config.tag.pygmentsfile.tab_width(8, :doc => 'Number of spaces used for a tabulator')
- config.tag.pygmentsfile.filename(nil, :doc => 'The source file', :mandatory => true)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement