tty_markdown 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/bin/tty_md ADDED
@@ -0,0 +1,6 @@
1
+ #! /usr/bin/env ruby
2
+ require_relative '../lib/tty_markdown'
3
+
4
+ colorizer = Redcarpet::Render::ColorDown
5
+ markdown = Redcarpet::Markdown.new(colorizer, fenced_code_blocks: true)
6
+ puts markdown.render ARGF.read
data/lib/ruby_color.rb ADDED
@@ -0,0 +1,44 @@
1
+ require 'syntax/convertors/html'
2
+ require 'colorize'
3
+
4
+ module ColorCode
5
+ class Ruby
6
+ TOKENIZER = Syntax.load "ruby"
7
+
8
+ def initialize(text)
9
+ @start_line = 1
10
+ @text = text
11
+ end
12
+
13
+ def colorize
14
+ @text.lines.map.with_index do |line, idx|
15
+ line_no = idx + @start_line
16
+ code_line = ""
17
+ TOKENIZER.tokenize( line ) do |token|
18
+ case token.group.to_s
19
+ when "string" then code_line += token.colorize(:light_yellow)
20
+ when "ident" then code_line += token.colorize(:white)
21
+ when "normal" then code_line += token.colorize(:cyan)
22
+ when "keyword" then code_line += token.colorize(:blue)
23
+ when "punct" then code_line += token.colorize(:white)
24
+ when "symbol" then code_line += token.colorize(:light_black)
25
+ when "number" then code_line += token.colorize(:light_black)
26
+ when "expr" then code_line += token.colorize(:white)
27
+ when "comment" then code_line += token.colorize(:magenta)
28
+ when "constant" then code_line += token.colorize(:yellow)
29
+ else code_line += token.group.to_s
30
+ end
31
+ end
32
+ (line_no.to_s.rjust(4) + ": ").colorize(:light_black) + code_line
33
+ end.join()
34
+ end
35
+ end
36
+ end
37
+
38
+ # Color Choices:
39
+ #[:black , :red , :green , :yellow ,
40
+ # :blue , :magenta , :cyan , :white ,
41
+ # :default , :light_black , :light_red , :light_green ,
42
+ # :light_yellow , :light_blue , :light_magenta ,
43
+ # :light_cyan , :light_white ]
44
+
@@ -0,0 +1,112 @@
1
+ #! /usr/bin/env ruby
2
+
3
+ require 'redcarpet'
4
+ require_relative './ruby_color'
5
+ require_relative './unknown_color'
6
+
7
+ module Redcarpet
8
+ module Render
9
+ class ColorDown < Base
10
+ UNORDERED = /unordered/
11
+ RUBY_LANG = /ruby/
12
+
13
+ COLOR_OFF = "\033[0m"
14
+ WHITE = "\033[0;107m"
15
+ RED = "\033[0;31m"
16
+ GREEN = "\033[0;32m"
17
+ YELLOW = "\033[0;33m"
18
+ L_GRAY = "\033[0;37m"
19
+ CYAN = "\033[0;36m"
20
+ BLUE_BG = "\033[0;44m"
21
+ GRAY_BG = "\033[0;100m"
22
+ ORANGE = "\033[38;5;136m"
23
+ ROSE = "\033[38;5;167m"
24
+ BOLD = "\033[0;01m"
25
+ DIM = "\033[0;02m"
26
+ UNDERSCORE = "\033[0;04m"
27
+ BINK = "\033[0;05m"
28
+
29
+ # Methods where the first argument is the text content
30
+ [
31
+ # block-level calls
32
+ :block_html,
33
+
34
+ # span-level calls
35
+ :autolink, :double_emphasis,
36
+ :underline, :raw_html,
37
+ :triple_emphasis, :strikethrough,
38
+ :superscript,
39
+
40
+ # footnotes
41
+ :footnotes, :footnote_def, :footnote_ref,
42
+
43
+ # low level rendering
44
+ :entity, :normal_text
45
+ ].each do |method|
46
+ define_method method do |*args|
47
+ args.first
48
+ end
49
+ end
50
+
51
+ # Other methods where the text content is in another argument
52
+ #def link(link, title, content)
53
+ #content
54
+ #end
55
+
56
+ def block_code(text, lang)
57
+ result = "```#{lang}\n"
58
+ if lang.match RUBY_LANG
59
+ result << ColorCode::Ruby.new(text).colorize
60
+ else
61
+ result << ColorCode::Unknown.new(text).colorize
62
+ end
63
+ result << "```\n"
64
+ result
65
+ end
66
+
67
+ def codespan(text)
68
+ GRAY_BG + "`#{text}`" + COLOR_OFF
69
+ end
70
+
71
+ def emphasis(text)
72
+ [ BOLD, '*', text,
73
+ '*', COLOR_OFF ].inject("") { |memo, v| memo << v.to_s }
74
+ end
75
+
76
+ def header(text, header_level)
77
+ heads = "".tap {|h| header_level.times {h << "#"} }
78
+ heads << " "
79
+ op = [ BLUE_BG, "\n", heads, text,
80
+ COLOR_OFF, "\n" ].inject("") { |memo, v| memo << v.to_s }
81
+ end
82
+
83
+ def list(text, type)
84
+ text.lines.inject("") do |memo, line|
85
+ memo << " " + line
86
+ end + "\n"
87
+ end
88
+
89
+ def list_item(text, type)
90
+ if type.match UNORDERED
91
+ "#{ORANGE}- #{COLOR_OFF}" + text
92
+ else
93
+ text
94
+ end
95
+ end
96
+
97
+ def paragraph(text)
98
+ "\n" + text + "\n"
99
+ end
100
+
101
+ def block_quote(text)
102
+ [ ROSE, "> ", text.strip,
103
+ COLOR_OFF, "\n" ].inject("") { |memo, v| memo << v.to_s }
104
+ end
105
+
106
+ def hrule
107
+ "\n----"
108
+ end
109
+
110
+ end
111
+ end
112
+ end
@@ -0,0 +1,16 @@
1
+ module ColorCode
2
+ class Unknown
3
+
4
+ def initialize(text)
5
+ @start_line = 1
6
+ @text = text
7
+ end
8
+
9
+ def colorize
10
+ @text.lines.map.with_index do |line, idx|
11
+ line_no = idx + @start_line
12
+ (line_no.to_s.rjust(4) + ": ").colorize(:light_black) + line
13
+ end.join()
14
+ end
15
+ end
16
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tty_markdown
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Mark Lorenz
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-09-14 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: colorize
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - '='
20
+ - !ruby/object:Gem::Version
21
+ version: 0.5.8
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - '='
28
+ - !ruby/object:Gem::Version
29
+ version: 0.5.8
30
+ - !ruby/object:Gem::Dependency
31
+ name: redcarpet
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - '='
36
+ - !ruby/object:Gem::Version
37
+ version: 3.0.0
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - '='
44
+ - !ruby/object:Gem::Version
45
+ version: 3.0.0
46
+ - !ruby/object:Gem::Dependency
47
+ name: syntax
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - '='
52
+ - !ruby/object:Gem::Version
53
+ version: 1.0.0
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - '='
60
+ - !ruby/object:Gem::Version
61
+ version: 1.0.0
62
+ description: reads a file or STDIN and outputs colorized markdown to STDOUT
63
+ email: markjlorenz@gmail.com
64
+ executables:
65
+ - tty_md
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - lib/tty_markdown.rb
70
+ - lib/ruby_color.rb
71
+ - lib/unknown_color.rb
72
+ - bin/tty_md
73
+ homepage: http://github.com/dapplebeforedawn
74
+ licenses:
75
+ - MIT
76
+ post_install_message:
77
+ rdoc_options: []
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ none: false
88
+ requirements:
89
+ - - ! '>='
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubyforge_project:
94
+ rubygems_version: 1.8.23
95
+ signing_key:
96
+ specification_version: 3
97
+ summary: colorized markdown up in your tty
98
+ test_files: []
99
+ has_rdoc: