r = Nginx::Request.new m = Discount.new("/st/style.css", "README") filename = r.filename filename = File.join(filename, 'README.md') if filename.end_with?('/') markdown = File.exists?(filename) ? File.read(filename) : '' Nginx.rputs m.header Nginx.rputs m.md2html(markdown) Nginx.rputs m.footer
location ~ \.md$ { add_header Content-Type text/html; mruby_content_handler "/opt/app/parse_md.rb" cache; }
module CGI TABLE_FOR_ESCAPE_HTML__ = {"&"=>"&", '"'=>""", "<"=>"<", ">"=>">"} def self.escapeHTML(string) string.gsub(/[&\"<>]/) do |ch| TABLE_FOR_ESCAPE_HTML__[ch] end end end class String def ord self.bytes[0] end end class Chalk COMMENT_START_CHARS = { ruby: /#./, cpp: /\/\*|\/\//, c: /\/\// } COMMENT_END_CHARS = { cpp: /\*\/|.\n/, ruby: /.\n/, c: /.\n/, } STRING_SEP = %w(' ") SEPARATORS = " @(){}[],.:;\"\'`<>=+-*/\t\n\\?|&#" SEPARATORS_RX = /[@\(\)\{\}\[\],\.\:;"'`\<\>=\+\-\*\/\t\n\\\?\|\&#]/ def initialize(file) @filename = file @file = File.new(file) @rnd = Random.new(file.hash) @tokens = {} reset end def parse &block reset() @file.read.each_char do |char| @last_couple = ((@last_couple.size < 2) ? @last_couple : @last_couple[1]) + char case(@state) when :source if start_comment?(@last_couple) @state = :comment elsif STRING_SEP.include?(char) @string_started_with = char @state = :string else process_entity(&block) if (@entity.length == 1 && SEPARATORS.index(@entity)) || SEPARATORS.index(char) end when :comment process_entity(:source, &block) if end_comment?(@last_couple) when :string if (STRING_SEP.include?(char) && @string_started_with == char) @entity += char process_entity(:source, &block) char = '' elsif char == '\\' @state = :escaped_char else end when :escaped_char @state = :string end @entity += char end end def to_html(&block) html = '' if block block.call( '<table><tr><td><pre>' ) else html = '<table><tr><td><pre>' end line_n = 1 @file.readlines.each do if block block.call( "<a href='#'><b>#{line_n}</b></a>\n" ) else html += "<a href='#'><b>#{line_n}</b></a>\n" end line_n += 1 end @file = File.open(@filename) if block block.call( '</pre></td><td><pre>' ) else html += '</pre></td><td><pre>' end parse do |entity, type| entity = entity.gsub("\t", ' ') if block block.call( entity ) #block.call(highlight( entity , type)) else html += entity #html += highlight( entity , type) end end if block block.call( '</pre><td></tr></table>' ) else html + '</pre><td></tr></table>' end end def language @language ||= case(@file.path.to_s.split('.').last.to_sym) when :rb :ruby when :cpp, :hpp :cpp when :c, :h :c when :py :python else @file.path.to_s.split('.').last.to_s end end private def process_entity(new_state = nil, &block) block.call @entity, @state if block @entity = '' @state = new_state if new_state end def reset @file = File.open(@filename) if @file @state = :source @string_started_with = '' @entity = '' @last_couple = '' end def color(entity) entity = entity.strip entity.gsub! SEPARATORS_RX, '' token = '' return token if entity.empty? #return token if token = @tokens[entity] return '' if entity[0].ord >= 128 rgb = [ @rnd.rand(150) + 100, @rnd.rand(150) + 100, @rnd.rand(150) + 100 ] token = String.sprintf("#%02X%02X%02X", rgb[0], rgb[1], rgb[2]) #token = "#%02X%02X%02X" % rgb #@tokens[entity] = token return token end def highlight(entity, type) esc_entity = CGI.escapeHTML( entity ) case type when :string, :comment "<span class='#{type}'>#{esc_entity}</span>" else rgb = color(entity) if rgb.empty? esc_entity else "<span rel='t#{rgb.hash}' style='color: #{rgb}' >#{esc_entity}</span>" end end end def start_comment?(char) rx = COMMENT_START_CHARS[language] char.match rx if rx end def end_comment?(char) rx = COMMENT_END_CHARS[language] char.match rx if rx end end
r = Nginx::Request.new Nginx.rputs '<html><link rel="stylesheet" href="/st/code.css" type="text/css" /><body>' begin ch = Chalk.new(r.filename) data = ch.to_html Nginx.rputs data rescue => e Nginx.rputs e.message end Nginx.rputs '</body></html>'
location ~ \.rb$ { add_header Content-Type text/html; mruby_content_handler "/opt/app/parse_code.rb" cache; }
Source: https://habr.com/ru/post/225313/
All Articles