tty-markdown 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'pastel'
4
+ require 'rouge'
5
+ require 'tty-color'
6
+
7
+ module TTY
8
+ module Markdown
9
+ # Syntax highlighting for terminal code snippets
10
+ #
11
+ # @api private
12
+ module SyntaxHighliter
13
+ # Return all available language lexers
14
+ #
15
+ # @return [Array[String]]
16
+ #
17
+ # @api private
18
+ def available_lexers
19
+ Rouge::Lexer.all.sort_by(&:tag).inject([]) do |names, lexer|
20
+ names << lexer.tag
21
+ if lexer.aliases.any?
22
+ lexer.aliases.each { |a| names << a }
23
+ end
24
+ names
25
+ end
26
+ end
27
+ module_function :available_lexers
28
+
29
+ # Guess langauge from code snippet
30
+ #
31
+ # @return [String, nil]
32
+ #
33
+ # @api private
34
+ def guess_lang(code)
35
+ lang = nil
36
+ start_line = code.lines[0]
37
+ if available_lexers.include?(start_line.strip.downcase)
38
+ lang = start_line.strip.downcase
39
+ end
40
+ end
41
+ module_function :guess_lang
42
+
43
+ # Highlight code snippet
44
+ #
45
+ # @api public
46
+ def highlight(code, **options)
47
+ lang = guess_lang(code)
48
+ mode = options[:mode] || TTY::Color.mode
49
+ lines = code.dup.lines
50
+ code = lines[1...-1].join + lines[-1].strip
51
+
52
+ lexer = Rouge::Lexer.find_fancy(lang, code) || Rouge::Lexers::PlainText
53
+
54
+ if mode >= 256
55
+ formatter = Rouge::Formatters::Terminal256.new
56
+ formatter.format(lexer.lex(code))
57
+ else
58
+ pastel = Pastel.new
59
+ code.split("\n").map { |line| pastel.yellow(line) }.join("\n")
60
+ end
61
+ end
62
+ module_function :highlight
63
+ end # SyntaxHighlighter
64
+ end # Markdown
65
+ end # TTY
@@ -0,0 +1,5 @@
1
+ module TTY
2
+ module Markdown
3
+ VERSION = '0.1.0'.freeze
4
+ end # TTY
5
+ end # Markdown
@@ -0,0 +1,11 @@
1
+ # encoding: utf-8
2
+
3
+ desc 'Load gem inside irb console'
4
+ task :console do
5
+ require 'irb'
6
+ require 'irb/completion'
7
+ require File.join(__FILE__, '../../lib/tty-markdown')
8
+ ARGV.clear
9
+ IRB.start
10
+ end
11
+ task c: %w[ console ]
@@ -0,0 +1,11 @@
1
+ # encoding: utf-8
2
+
3
+ desc 'Measure code coverage'
4
+ task :coverage do
5
+ begin
6
+ original, ENV['COVERAGE'] = ENV['COVERAGE'], 'true'
7
+ Rake::Task['spec'].invoke
8
+ ensure
9
+ ENV['COVERAGE'] = original
10
+ end
11
+ end
data/tasks/spec.rake ADDED
@@ -0,0 +1,29 @@
1
+ # encoding: utf-8
2
+
3
+ begin
4
+ require 'rspec/core/rake_task'
5
+
6
+ desc 'Run all specs'
7
+ RSpec::Core::RakeTask.new(:spec) do |task|
8
+ task.pattern = 'spec/{unit,integration}{,/*/**}/*_spec.rb'
9
+ end
10
+
11
+ namespace :spec do
12
+ desc 'Run unit specs'
13
+ RSpec::Core::RakeTask.new(:unit) do |task|
14
+ task.pattern = 'spec/unit{,/*/**}/*_spec.rb'
15
+ end
16
+
17
+ desc 'Run integration specs'
18
+ RSpec::Core::RakeTask.new(:integration) do |task|
19
+ task.pattern = 'spec/integration{,/*/**}/*_spec.rb'
20
+ end
21
+ end
22
+
23
+ rescue LoadError
24
+ %w[spec spec:unit spec:integration].each do |name|
25
+ task name do
26
+ $stderr.puts "In order to run #{name}, do `gem install rspec`"
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,33 @@
1
+ lib = File.expand_path("../lib", __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "tty/markdown/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "tty-markdown"
7
+ spec.version = TTY::Markdown::VERSION
8
+ spec.authors = ["Piotr Murach"]
9
+ spec.email = []
10
+
11
+ spec.summary = %q{Convert a markdown text or document into a terminal friendly output.}
12
+ spec.description = %q{Convert a markdown text or document into a terminal friendly output.}
13
+ spec.homepage = "https://piotrmurach.github.io/tty"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_dependency "kramdown", '~> 1.16.2'
24
+ spec.add_dependency "pastel", '~> 0.7.2'
25
+ spec.add_dependency "rouge", '~> 3.1.0'
26
+ spec.add_dependency "strings", '~> 0.1.0'
27
+ spec.add_dependency "tty-color", '~> 0.4.2'
28
+ spec.add_dependency "tty-screen", '~> 0.6.2'
29
+
30
+ spec.add_development_dependency "bundler", "~> 1.16"
31
+ spec.add_development_dependency "rake", "~> 10.0"
32
+ spec.add_development_dependency "rspec", "~> 3.0"
33
+ end
metadata ADDED
@@ -0,0 +1,197 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tty-markdown
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Piotr Murach
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-01-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: kramdown
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 1.16.2
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 1.16.2
27
+ - !ruby/object:Gem::Dependency
28
+ name: pastel
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.7.2
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.7.2
41
+ - !ruby/object:Gem::Dependency
42
+ name: rouge
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 3.1.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 3.1.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: strings
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.1.0
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.1.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: tty-color
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 0.4.2
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 0.4.2
83
+ - !ruby/object:Gem::Dependency
84
+ name: tty-screen
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 0.6.2
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 0.6.2
97
+ - !ruby/object:Gem::Dependency
98
+ name: bundler
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '1.16'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '1.16'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rake
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '10.0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '10.0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: rspec
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '3.0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '3.0'
139
+ description: Convert a markdown text or document into a terminal friendly output.
140
+ email: []
141
+ executables: []
142
+ extensions: []
143
+ extra_rdoc_files: []
144
+ files:
145
+ - ".gitignore"
146
+ - ".rspec"
147
+ - ".travis.yml"
148
+ - CHANGELOG.md
149
+ - CODE_OF_CONDUCT.md
150
+ - Gemfile
151
+ - LICENSE.txt
152
+ - README.md
153
+ - Rakefile
154
+ - appveyor.yml
155
+ - assets/headers.png
156
+ - assets/list.png
157
+ - assets/quote.png
158
+ - assets/syntax_highlight.png
159
+ - assets/table.png
160
+ - bin/console
161
+ - bin/setup
162
+ - examples/example.md
163
+ - examples/marked.rb
164
+ - lib/tty-markdown.rb
165
+ - lib/tty/markdown.rb
166
+ - lib/tty/markdown/parser.rb
167
+ - lib/tty/markdown/syntax_highlighter.rb
168
+ - lib/tty/markdown/version.rb
169
+ - tasks/console.rake
170
+ - tasks/coverage.rake
171
+ - tasks/spec.rake
172
+ - tty-markdown.gemspec
173
+ homepage: https://piotrmurach.github.io/tty
174
+ licenses:
175
+ - MIT
176
+ metadata: {}
177
+ post_install_message:
178
+ rdoc_options: []
179
+ require_paths:
180
+ - lib
181
+ required_ruby_version: !ruby/object:Gem::Requirement
182
+ requirements:
183
+ - - ">="
184
+ - !ruby/object:Gem::Version
185
+ version: '0'
186
+ required_rubygems_version: !ruby/object:Gem::Requirement
187
+ requirements:
188
+ - - ">="
189
+ - !ruby/object:Gem::Version
190
+ version: '0'
191
+ requirements: []
192
+ rubyforge_project:
193
+ rubygems_version: 2.5.1
194
+ signing_key:
195
+ specification_version: 4
196
+ summary: Convert a markdown text or document into a terminal friendly output.
197
+ test_files: []