lib/patricia/app.rb in patricia-0.0.1 vs lib/patricia/app.rb in patricia-0.1.1

- old
+ new

@@ -1,7 +1,9 @@ require 'sinatra/base' require 'yaml' +require 'json' +require 'timeout' module PatriciaApp class App < Sinatra::Base @@ -34,10 +36,12 @@ set :app_markdown_glob, '.{md,markdown,org,textile,rst,rest,restx}' set :app_css_path, '/patricia.css' set :app_js_path, '/patricia.js' set :app_public_folder, app_configs[:public_folder] set :app_tooltips, app_configs[:tooltips] + set :app_editor, app_configs[:editor] + set :app_gfm, app_configs[:gfm] # CSS if app_configs[:css_dir] != nil set :app_css_dir, app_configs[:css_dir] css_paths = Dir[app_configs[:css_dir] + '/**/*.css'] .collect do |path| @@ -77,14 +81,16 @@ 'gzip' => 'application/gzip', 'pdf' => 'application/pdf', 'sh' => 'application/x-sh', '7z' => 'application/x-7z-compressed', 'swf' => 'application/x-shockwave-flash', + 'xml' => 'application/xml;charset=utf-8', 'avi' => 'video/x-msvideo', 'txt' => 'text/plain', 'css' => 'text/css', 'csv' => 'text/csv', + 'html' => 'text/html', # Send markup source files # # One can argue if this is semantically correct (alternative: # `plain/x-markdown;charset=UTF-8'), but as there is no official # final specification and this solution comes with the highest @@ -119,12 +125,18 @@ end.join(' ') end end get '/' do - @html = Patricia::Converter .to_html(File.dirname(__FILE__) + + if settings.app_gfm + html = Patricia::Converter.gfm_to_html(File.dirname(__FILE__) + + '/views/wiki/welcome.md') + else + html = Patricia::Converter.to_html(File.dirname(__FILE__) + '/views/wiki/welcome.md') + end + @html = '<div id="p-welcome-text-page" class="">' + html + '</div>' @toc = build_toc @title = generate_page_title @page_title = '' @breadcrumb = '' @stylesheets = settings.app_css @@ -159,22 +171,68 @@ file_name = File.basename(p, '.*') no_ext = File.join(File.dirname(p), file_name).sub(/^\.\//, '') beautiful_file_name = capitalize_all(file_name.gsub(/-/, ' ')) beautiful_path = no_ext.split('/').collect do |s| capitalize_all(s.gsub(/-/, ' ')) - end.join(' > ') + end.delete_if(&:empty?).join(' > ') content = File.read(path) lines = content.split("\n").length if content =~ search_query - @results << [beautiful_file_name, '/' + no_ext, beautiful_path, + @results << [beautiful_file_name, no_ext, beautiful_path, lines] end end end haml :search, :layout => :application end + post %r{/patricia/offsets/?} do + if settings.app_editor + file_content = File.read(File.join(settings.app_markup_dir, + params[:markup_url])) + offsets = [] + # Time out if the RegEx is too complex. + begin + Timeout::timeout(3) do + filler = " *\n*\w*" + pattern = Regexp.new(filler, Regexp::MULTILINE) + params[:string].split.each do |token| + pattern = + Regexp.new(pattern.to_s + filler + Regexp.quote(token), + Regexp::MULTILINE) + end + pattern = Regexp.new(pattern.to_s + filler, Regexp::MULTILINE) + + # Skip empty/whitespace only queries. + if !pattern.to_s.empty? && !(pattern.to_s =~ /^\s*$/) + file_content.scan(pattern) do |c| + offsets << [$~.offset(0)[0], $~.offset(0)[1]] + end + end + end + rescue + # Ignore. + end + + content_type 'json' + {:markup => file_content, :offsets => offsets}.to_json + else + redirect to('/404') + end + end + + post %r{/patricia/edit/?} do + if settings.app_editor + File.open(File.join(settings.app_markup_dir, + params[:markup_url]), 'w') do |f| + f.puts(params[:string]) + end + else + redirect to('/404') + end + end + get settings.app_css_path do pwd = File.dirname(__FILE__) css = '' [ '/assets/stylesheets/bootstrap.min.css', @@ -194,10 +252,11 @@ '/assets/javascripts/jquery-1.11.0.min.js', '/assets/javascripts/bootstrap.min.js', '/assets/javascripts/app.js', ] files << '/assets/javascripts/tooltips.js' if settings.app_tooltips + files << '/assets/javascripts/editor.js' if settings.app_editor files.each do |path| js << File.read(pwd + path) + "\n\n" end content_type 'text/javascript' js @@ -213,10 +272,14 @@ # request. begin file_path = Dir[settings.app_markup_dir + '/' + path + settings.app_markdown_glob].first @markup_url = file_path.gsub(/#{settings.app_markup_dir}/, '') - @html = Patricia::Converter.to_html(file_path) + if settings.app_gfm + @html = Patricia::Converter.gfm_to_html(file_path) + else + @html = Patricia::Converter.to_html(file_path) + end @toc = build_toc arrow = ' > ' @title = generate_page_title @breadcrumb = capitalize_all(File.dirname(path).gsub(/\//, ' ')) breadcrumb_array = @breadcrumb.split