lib/kramdown/service.rb in kramdown-service-0.0.1 vs lib/kramdown/service.rb in kramdown-service-0.1.0
- old
+ new
@@ -6,21 +6,19 @@
# e.g. config.ru:
# require './boot'
# run Kramdown::Service
+require 'json'
+
+
# 3rd party libs/gems
require 'sinatra/base'
require 'kramdown'
-
-# require 'logutils'
-# require 'logutils/activerecord'
-
-
# our own code
require 'kramdown/service/version' # let version always go first
@@ -42,99 +40,159 @@
##############################################
# Controllers / Routing / Request Handlers
- def welcome_markdown
- ## todo: rotate welcome / use random number for index
- # place markdown docs in server/docs
- text = File.read( "#{KramdownService.root}/lib/markdown/service/docs/welcome.md" )
- text
- end
-
get %r{/(service|services|srv|s)$} do
+ ## just a "live" docu page
erb :service
end
- get %r{/(note|notes|n)$} do
- # for testing/debugging use copied sources 1:1 from markdown-notepad repo
- redirect '/note.html'
- end
-
get %r{/(editor|edit|ed|e)$} do
- # NB: use editor for "ruby-enhanced" parts of note
- @welcome_markdown = welcome_markdown
- @welcome_html = Kramdown::Document.new( @welcome_markdown ).to_html
+ # note: allow optional params e.g. text and opts
+ ## note: for now only html supported on get form/url params
+ text = params.delete('text') || welcome_markdown
+ to = params.delete('to') || 'html' ## optional - default to html
+ opts = params_to_opts( params )
+ @welcome_markdown = text
+ @welcome_html = text_to_html( text, opts )
+
erb :editor
end
get '/' do
- @welcome_markdown = welcome_markdown
- @welcome_html = Kramdown::Document.new( @welcome_markdown ).to_html
+ # note: allow optional params e.g. text and opts
+ ## note: for now only html supported on get form/url params
+ text = params.delete('text') || welcome_markdown
+ to = params.delete('to') || 'html' ## optional - default to html
+ opts = params_to_opts( params )
+ @welcome_markdown = text
+ @welcome_html = text_to_html( text, opts )
+
erb :index
end
+ # return hypertext (html) ## allow /markdown or /m
+ get %r{/(markdown|m)$} do
- ## todo: use 3rd party services from markdown.yml (lets you configure)
- # e.g. http://johnmacfarlane.net/cgi-bin/pandoc-dingus?text=hi
+ text = params.delete('text')
+ to = params.delete('to') || 'html' ## optional - default to html
+ opts = params_to_opts( params )
+
+ if ['latex','l','tex'].include?( to.downcase )
+ content_type 'text/latex'
+ text_to_latex( text, opts )
+ else ## assume html (default)
+ content_type 'text/html'
+ text_to_html( text, opts )
+ end
+
+ end
- def markdownify( params, opts={} )
- pp params
- text = params[:text]
- lib = params[:lib] # optional
- pp text
- pp lib
-
- Kramdown::Document.new( text, opts ).to_html
- end
-
-
# return babelmark2/dingus-style json
- get '/markdown/dingus' do
- html = markdownify( params )
+ # return html wrapped in json (follows babelmark2 dingus service api)
+ # note: defaults (uses) GFM - github-flavored markdown mode/input
+ # note: only supports html for now (e.g. does NOT support to=html|latex option etc.)
+ get '/babelmark' do
+ text = params.delete('text')
data = {
- name: 'kramdown',
- html: html,
+ name: 'kramdown',
+ html: Kramdown::Document.new( text, input: 'GFM' ).to_html,
version: Kramdown::VERSION
}
json_or_jsonp( data.to_json )
end
- # return hypertext (html)
- get '/markdown' do
- content_type 'text/html'
- markdownify( params )
+ get '/d*' do
+ erb :debug
end
- # return html wrapped in json (follows babelfish2 dingus service api)
- get '/dingus' do
- html = markdownify( params )
+
+private
+
+ def welcome_markdown
+ ## todo: rotate welcome / use random number for index
+ # place markdown docs in server/docs
+ text = File.read( "#{KramdownService.root}/lib/kramdown/service/docs/welcome.md" )
+ text
+ end
+
+ def params_to_opts( params )
+ ## convert (web form) params to kramdown (ruby) opts
+
+ puts "params : #{params.class.name}:"
+ pp params
+
+ opts = {}
+
+ ## map true/false strings to boolean
+ params.each do |k,v|
+ puts " k: >#{k}< : #{k.class.name}, v: >#{v}< : #{v.class.name}"
+
+ ## skip "built-in" sinatra "internal" params
+ ## - todo - use splice and whitelist instead - why? why not?
+ next if ['splat', 'captures'].include?( k )
- data = {
- name: 'kramdown',
- html: html,
- version: Kramdown::Version
- }
-
- json_or_jsonp( data.to_json )
+ if v.is_a?( String ) && ['t', 'true'].include?( v.downcase )
+ opts[ k ] = true
+ elsif v.is_a?( String ) && ['f', 'false'].include?( v.downcase )
+ opts[ k ] = false
+ else
+ opts[ k ] = v
+ end
+ end
+
+ opts
end
- get '/d*' do
- erb :debug
+ def preprocess_opts( opts )
+ ### special case for input opt
+ ## always default to gfm (github-flavored markdown) for now
+
+ input = opts.delete( 'input' ) || 'GFM'
+ if ['classic', 'std', 'standard', 'kramdown' ].include?( input.downcase )
+ ## skip; "pseudo" input options map to no (zero) standard/classic input
+ else
+ opts[ 'input' ] = input
+ end
+
+ puts "opts (preprocessed/effective):"
+ pp opts
+
+ opts
end
+ def text_to_html( text, opts={} )
+ puts "text_to_html:"
+ pp text
+ pp opts
+ opts = preprocess_opts( opts ) ## defaults to GFM input etc.
+
+ Kramdown::Document.new( text, opts ).to_html
+ end
+
+ def text_to_latex( text, opts={} )
+ puts "text_to_latex:"
+ pp text
+ pp opts
+
+ opts = preprocess_opts( opts ) ## defaults to GFM input etc.
+
+ Kramdown::Document.new( text, opts ).to_latex
+ end
+
+
### helper for json or jsonp response (depending on callback para)
-private
def json_or_jsonp( json )
callback = params.delete('callback')
response = ''
if callback