kramdown-service 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 495b9daefa7ff13409c0996204dd715c99242ec8
4
- data.tar.gz: 40cc00e2fd372db7d07684ee4bfb71d4d22fe56f
3
+ metadata.gz: 065d0c7fe06c3a2639983020f3a36bf20cf91850
4
+ data.tar.gz: c93f8547c5059f71d79fbb9bbaae3e2da700026a
5
5
  SHA512:
6
- metadata.gz: dab21da522d3036036d084dc39d938092ebb1a5416292672be419695b67ada966ce320146ee343740ce06526345b1503ee32f5b937520454568d7b5330427306
7
- data.tar.gz: 64524647d259636125214bbb464f58cb84c8b2ab283c4887f1ba7e475ea6271265938705af656a2949fee19a871b2aeeead662fd2773c72afdf2931eb782b117
6
+ metadata.gz: f55ec265130e94db23e8a3fcee6a5914ac2b270649476f8084ea4e1bb4be6be44b3a176bb78ea68fabb69c0ffd8663764e813dda3b3b729cc0ad6ace42faff3b
7
+ data.tar.gz: 4d5137c11efb8524a441427803037000cd94e7b5f0c3cd7b63afac9dfa8d931bff388bc07323861001354399bbfc952dcceca43c66a014f5c6fb22804c64b863
@@ -7,13 +7,9 @@ lib/kramdown/service/docs/welcome.md
7
7
  lib/kramdown/service/public/css/markdown/note.css
8
8
  lib/kramdown/service/public/css/markdown/themes/basic.css
9
9
  lib/kramdown/service/public/i/dots-white.gif
10
+ lib/kramdown/service/public/js/kramdown.js
10
11
  lib/kramdown/service/public/js/lib3rd/jquery-2.0.1.min.js
11
- lib/kramdown/service/public/js/lib3rd/pagedown.js
12
- lib/kramdown/service/public/js/lib3rd/showdown.min.js
13
- lib/kramdown/service/public/js/markdown.api.js
14
- lib/kramdown/service/public/js/markdown.lib.js
15
12
  lib/kramdown/service/public/js/markdown.note.js
16
- lib/kramdown/service/public/note.html
17
13
  lib/kramdown/service/public/style.css
18
14
  lib/kramdown/service/version.rb
19
15
  lib/kramdown/service/views/_about.erb
data/README.md CHANGED
@@ -11,6 +11,7 @@
11
11
  Try the `markdown` HTTP (JSON) API running
12
12
  on Heroku [`trykramdown.herokuapp.com`](http://trykramdown.herokuapp.com).
13
13
 
14
+
14
15
  Example 1 - Converting to Hypertext (HTML):
15
16
 
16
17
  GET /markdown?text=Hello+World!
@@ -20,20 +21,11 @@ Example 1 - Converting to Hypertext (HTML):
20
21
 
21
22
  Example 2 - Converting to LaTeX:
22
23
 
23
- GET /markdown?text=Hello+World!&format=latex
24
+ GET /markdown?text=Hello+World!&to=latex
24
25
 
25
26
  Hello World!
26
27
 
27
- <!-- todo/check:
28
- - use format or output ??? for parameter
29
- -->
30
-
31
-
32
-
33
- ## Dependencies / Building Blocks
34
28
 
35
- [Markdown Note](https://github.com/writekit/markdown-note) - Another simple single-page, server-less Markdown editor
36
- in JavaScript & Hypertext.
37
29
 
38
30
 
39
31
  ## License
data/Rakefile CHANGED
@@ -14,7 +14,8 @@ Hoe.spec 'kramdown-service' do
14
14
  self.email = 'wwwmake@googlegroups.com'
15
15
 
16
16
  self.extra_deps = [
17
- ['kramdown'],
17
+ ['kramdown'], ## markdown converter
18
+ ['rouge'], ## syntax highlighter
18
19
  ['sinatra'],
19
20
  ]
20
21
 
@@ -8,17 +8,15 @@
8
8
  # run Kramdown::Service
9
9
 
10
10
 
11
+ require 'json'
12
+
13
+
11
14
  # 3rd party libs/gems
12
15
 
13
16
  require 'sinatra/base'
14
17
 
15
18
  require 'kramdown'
16
19
 
17
-
18
- # require 'logutils'
19
- # require 'logutils/activerecord'
20
-
21
-
22
20
  # our own code
23
21
 
24
22
  require 'kramdown/service/version' # let version always go first
@@ -44,61 +42,66 @@ class Service < Sinatra::Base
44
42
  ##############################################
45
43
  # Controllers / Routing / Request Handlers
46
44
 
47
- def welcome_markdown
48
- ## todo: rotate welcome / use random number for index
49
- # place markdown docs in server/docs
50
- text = File.read( "#{KramdownService.root}/lib/markdown/service/docs/welcome.md" )
51
- text
52
- end
53
-
54
45
 
55
46
  get %r{/(service|services|srv|s)$} do
47
+ ## just a "live" docu page
56
48
  erb :service
57
49
  end
58
50
 
59
- get %r{/(note|notes|n)$} do
60
- # for testing/debugging use copied sources 1:1 from markdown-notepad repo
61
- redirect '/note.html'
62
- end
63
-
64
51
  get %r{/(editor|edit|ed|e)$} do
65
- # NB: use editor for "ruby-enhanced" parts of note
66
- @welcome_markdown = welcome_markdown
67
- @welcome_html = Kramdown::Document.new( @welcome_markdown ).to_html
52
+ # note: allow optional params e.g. text and opts
53
+ ## note: for now only html supported on get form/url params
54
+ text = params.delete('text') || welcome_markdown
55
+ to = params.delete('to') || 'html' ## optional - default to html
56
+ opts = params_to_opts( params )
57
+
58
+ @welcome_markdown = text
59
+ @welcome_html = text_to_html( text, opts )
68
60
 
69
61
  erb :editor
70
62
  end
71
63
 
72
64
  get '/' do
73
- @welcome_markdown = welcome_markdown
74
- @welcome_html = Kramdown::Document.new( @welcome_markdown ).to_html
65
+ # note: allow optional params e.g. text and opts
66
+ ## note: for now only html supported on get form/url params
67
+ text = params.delete('text') || welcome_markdown
68
+ to = params.delete('to') || 'html' ## optional - default to html
69
+ opts = params_to_opts( params )
70
+
71
+ @welcome_markdown = text
72
+ @welcome_html = text_to_html( text, opts )
75
73
 
76
74
  erb :index
77
75
  end
78
76
 
79
-
80
- ## todo: use 3rd party services from markdown.yml (lets you configure)
81
- # e.g. http://johnmacfarlane.net/cgi-bin/pandoc-dingus?text=hi
82
-
83
-
84
- def markdownify( params, opts={} )
85
- pp params
86
- text = params[:text]
87
- lib = params[:lib] # optional
88
- pp text
89
- pp lib
90
-
91
- Kramdown::Document.new( text, opts ).to_html
92
- end
77
+ # return hypertext (html) ## allow /markdown or /m
78
+ get %r{/(markdown|m)$} do
79
+
80
+ text = params.delete('text')
81
+ to = params.delete('to') || 'html' ## optional - default to html
82
+ opts = params_to_opts( params )
83
+
84
+ if ['latex','l','tex'].include?( to.downcase )
85
+ content_type 'text/latex'
86
+ text_to_latex( text, opts )
87
+ else ## assume html (default)
88
+ content_type 'text/html'
89
+ text_to_html( text, opts )
90
+ end
91
+
92
+ end
93
93
 
94
94
 
95
95
  # return babelmark2/dingus-style json
96
- get '/markdown/dingus' do
97
- html = markdownify( params )
96
+ # return html wrapped in json (follows babelmark2 dingus service api)
97
+ # note: defaults (uses) GFM - github-flavored markdown mode/input
98
+ # note: only supports html for now (e.g. does NOT support to=html|latex option etc.)
99
+ get '/babelmark' do
100
+ text = params.delete('text')
98
101
 
99
102
  data = {
100
- name: 'kramdown',
101
- html: html,
103
+ name: 'kramdown',
104
+ html: Kramdown::Document.new( text, input: 'GFM' ).to_html,
102
105
  version: Kramdown::VERSION
103
106
  }
104
107
 
@@ -106,34 +109,89 @@ class Service < Sinatra::Base
106
109
  end
107
110
 
108
111
 
109
- # return hypertext (html)
110
- get '/markdown' do
111
- content_type 'text/html'
112
- markdownify( params )
112
+ get '/d*' do
113
+ erb :debug
113
114
  end
114
115
 
115
- # return html wrapped in json (follows babelfish2 dingus service api)
116
- get '/dingus' do
117
- html = markdownify( params )
116
+
117
+ private
118
+
119
+ def welcome_markdown
120
+ ## todo: rotate welcome / use random number for index
121
+ # place markdown docs in server/docs
122
+ text = File.read( "#{KramdownService.root}/lib/kramdown/service/docs/welcome.md" )
123
+ text
124
+ end
125
+
126
+ def params_to_opts( params )
127
+ ## convert (web form) params to kramdown (ruby) opts
128
+
129
+ puts "params : #{params.class.name}:"
130
+ pp params
131
+
132
+ opts = {}
133
+
134
+ ## map true/false strings to boolean
135
+ params.each do |k,v|
136
+ puts " k: >#{k}< : #{k.class.name}, v: >#{v}< : #{v.class.name}"
137
+
138
+ ## skip "built-in" sinatra "internal" params
139
+ ## - todo - use splice and whitelist instead - why? why not?
140
+ next if ['splat', 'captures'].include?( k )
118
141
 
119
- data = {
120
- name: 'kramdown',
121
- html: html,
122
- version: Kramdown::Version
123
- }
124
-
125
- json_or_jsonp( data.to_json )
142
+ if v.is_a?( String ) && ['t', 'true'].include?( v.downcase )
143
+ opts[ k ] = true
144
+ elsif v.is_a?( String ) && ['f', 'false'].include?( v.downcase )
145
+ opts[ k ] = false
146
+ else
147
+ opts[ k ] = v
148
+ end
149
+ end
150
+
151
+ opts
126
152
  end
127
153
 
128
154
 
129
- get '/d*' do
130
- erb :debug
155
+ def preprocess_opts( opts )
156
+ ### special case for input opt
157
+ ## always default to gfm (github-flavored markdown) for now
158
+
159
+ input = opts.delete( 'input' ) || 'GFM'
160
+ if ['classic', 'std', 'standard', 'kramdown' ].include?( input.downcase )
161
+ ## skip; "pseudo" input options map to no (zero) standard/classic input
162
+ else
163
+ opts[ 'input' ] = input
164
+ end
165
+
166
+ puts "opts (preprocessed/effective):"
167
+ pp opts
168
+
169
+ opts
170
+ end
171
+
172
+ def text_to_html( text, opts={} )
173
+ puts "text_to_html:"
174
+ pp text
175
+ pp opts
176
+
177
+ opts = preprocess_opts( opts ) ## defaults to GFM input etc.
178
+
179
+ Kramdown::Document.new( text, opts ).to_html
180
+ end
181
+
182
+ def text_to_latex( text, opts={} )
183
+ puts "text_to_latex:"
184
+ pp text
185
+ pp opts
186
+
187
+ opts = preprocess_opts( opts ) ## defaults to GFM input etc.
188
+
189
+ Kramdown::Document.new( text, opts ).to_latex
131
190
  end
132
191
 
133
192
 
134
193
  ### helper for json or jsonp response (depending on callback para)
135
194
 
136
- private
137
195
  def json_or_jsonp( json )
138
196
  callback = params.delete('callback')
139
197
  response = ''
@@ -1,8 +1,8 @@
1
- # Header 1
1
+ # Heading 1
2
2
 
3
- ## Header 2
3
+ ## Heading 2
4
4
 
5
- ### Header 3
5
+ ### Heading 3
6
6
 
7
7
  Welcome to [Markdown](/). We hope you **really** enjoy using this.
8
8
 
@@ -0,0 +1,64 @@
1
+
2
+
3
+ var kramdown_new = function( opts ) {
4
+
5
+ var settings; // NB: defaults + opts merged => settings
6
+
7
+ var defaults = {
8
+ api_url: 'http://trykramdown.herokuapp.com/markdown'
9
+ }
10
+
11
+
12
+ function _debug( msg )
13
+ {
14
+ if(window.console && window.console.log )
15
+ window.console.log( "[debug] " + msg );
16
+ }
17
+
18
+
19
+ function _init( opts ) {
20
+ settings = $.extend( {}, defaults, opts );
21
+ }
22
+
23
+ _init( opts );
24
+
25
+
26
+ function _convert( text, more_params, handler )
27
+ {
28
+ var params = $.extend( { text: text }, more_params ); // merge in more params; use text for required param
29
+
30
+ $.get( settings.api_url, params, function( data ) {
31
+ handler( data );
32
+ });
33
+ }
34
+
35
+ function convert_to_html( text, handler ) {
36
+ // note: make gfm (github-flavored markdown) the default parser
37
+ _convert( text, { to: 'html' }, handler );
38
+ }
39
+
40
+ function convert_to_html_with_syntax_highlighter( text, handler ) {
41
+ // note: make gfm (github-flavored markdown) the default parser
42
+ _convert( text, { to: 'html', syntax_highlighter: 'rouge' }, handler );
43
+ }
44
+
45
+ function convert_to_html_with_classic( text, handler ) {
46
+ // note: use "classic" "standard" kramdown parser/reader
47
+ _convert( text, { to: 'html', input: 'classic' }, handler );
48
+ }
49
+
50
+
51
+
52
+ function convert_to_latex( text, handler ) {
53
+ _convert( text, { to: 'latex' }, handler );
54
+ }
55
+
56
+ return {
57
+ convert_to_html: convert_to_html,
58
+ convert_to_html_with_syntax_highlighter: convert_to_html_with_syntax_highlighter,
59
+ convert_to_html_with_classic: convert_to_html_with_classic,
60
+ convert_to_latex: convert_to_latex
61
+ }
62
+
63
+ } // fn kramdown_new
64
+
@@ -4,15 +4,28 @@ var markdown_note_new = function( opts ) {
4
4
 
5
5
  // use module pattern (see JavaScript - The Good Parts)
6
6
 
7
+
8
+ var getUrl = window.location;
9
+ // note: host includes hostname + port; yes!
10
+ var baseUrl = getUrl.protocol + "//" + getUrl.host;
11
+
12
+ console.log( window.location );
13
+ console.log( baseUrl );
14
+
15
+
16
+ var kramdown = kramdown_new( { api_url: baseUrl + '/markdown' } );
17
+ // e.g. http://localhost:9292/markdown or http://trykramdown.herokuapp.com/markdown
18
+ // kramdown api service / engine (see kramdown.js)
19
+
7
20
  var engines = [
8
- { name: 'Standard (Offline)', online: false, markdown: markdown_libs.showdown },
9
- { name: 'Standard (Online)', online: true, markdown: markdown_apis.ruby },
10
- { name: 'JavaScript - Showdown (Offline)', online: false, markdown: markdown_libs.showdown },
11
- { name: 'JavaScript - pagedown (Offline)', online: false, markdown: markdown_libs.pagedown },
12
- { name: 'Ruby - kramdown (Online)', online: true, markdown: markdown_apis.ruby_kramdown },
13
- { name: 'Ruby - Redcarpet (Online)', online: true, markdown: markdown_apis.ruby_redcarpet },
14
- { name: 'Ruby - Maruku (Online)', online: true, markdown: markdown_apis.ruby_maruku },
15
- { name: 'Ruby - BlueCloth (Online)', online: true, markdown: markdown_apis.ruby_bluecloth }
21
+ { name: 'kramdown in GitHub-Flavored Markdown (GFM) Mode', format: 'HTML',
22
+ markdown: kramdown.convert_to_html },
23
+ { name: 'kramdown in GitHub-Flavored Markdown (GFM) Mode w/ Syntax Highlighter (rouge)', format: 'HTML',
24
+ markdown: kramdown.convert_to_html_with_syntax_highlighter },
25
+ { name: 'kramdown in "Classic" Mode', format: 'HTML',
26
+ markdown: kramdown.convert_to_html_with_classic },
27
+ { name: 'kramdown in GitHub-Flavored Markdown (GFM) Mode', format: 'LaTeX',
28
+ markdown: kramdown.convert_to_latex }
16
29
  ];
17
30
 
18
31
  var welcome = {
@@ -69,7 +82,29 @@ var markdown_note_new = function( opts ) {
69
82
 
70
83
  var show_html = false;
71
84
  var use_white_color_theme = false;
85
+ var show_latex = false;
86
+
87
+ function show_latex_output() {
88
+ show_latex = true;
72
89
 
90
+ $output.hide();
91
+ $output_toggle.hide(); // hide toggle show html button
92
+ $output_source.show();
93
+ }
94
+
95
+ function restore_html_output() {
96
+ // restore output view to before entering "latex" mode
97
+ if (show_latex == true) {
98
+ show_latex = false;
99
+
100
+ $output_toggle.show();
101
+ if( !show_html ) { // switch back in "preview" mode ?
102
+ $output_source.hide();
103
+ $output.show();
104
+ }
105
+ }
106
+ }
107
+
73
108
  function toggle_output()
74
109
  {
75
110
  show_html = !show_html;
@@ -112,13 +147,17 @@ var markdown_note_new = function( opts ) {
112
147
 
113
148
  var engine = settings.engines[engine_index];
114
149
 
115
- if( engine.online === true )
116
- $output_loading.show();
150
+ $output_loading.show(); // show progress bar/spinner
117
151
 
118
152
  engine.markdown( text, function( html ) {
119
153
  $output.html( html );
120
154
  $output_source.html( html );
121
155
  $output_loading.hide();
156
+
157
+ if( engine.format == 'LaTeX' ) // for latex always show source (only)
158
+ show_latex_output();
159
+ else
160
+ restore_html_output();
122
161
  });
123
162
  }
124
163
 
@@ -126,8 +165,7 @@ var markdown_note_new = function( opts ) {
126
165
  function _init( opts )
127
166
  {
128
167
  settings = $.extend( {}, defaults, opts );
129
-
130
-
168
+
131
169
  $output = $( settings.output );
132
170
  $output_source = $( settings.output_source );
133
171
  $output_update = $( settings.output_update );
@@ -157,7 +195,7 @@ var markdown_note_new = function( opts ) {
157
195
  if( index == 0 ) {
158
196
  markdown_opts += 'selected="selected"'
159
197
  }
160
- markdown_opts += '>' + engine.name + '</option>';
198
+ markdown_opts += '>' + engine.format + ' - ' + engine.name + '</option>';
161
199
 
162
200
  _debug( 'add markdown engine [' + index + ']: ' + engine.name );
163
201
  });