markdown_stream_formatter 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/lib/markdown_stream_formatter.rb +109 -0
- metadata +45 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 6ad7b8676a950bd675e3ebec91c9ec52e50ee5c239d4879f2e43813a93a04e11
|
4
|
+
data.tar.gz: 49ad80ea3b4e4e8c3ae8d86071ab436e5b9c38318c050581cc6f58c9912e3280
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 800d2fbbe57a58b855cb655ab3ef4cc006de064d82bf180067cdd7e239f4953ac5e60c6d6473824231626a21beb086c4a84f35ddd55994460cbfe996eeb29f08
|
7
|
+
data.tar.gz: ad6fbbb6d676a9388ba404f303947ffbd616f52900845a8531947fc8d5abe6ed7761b390cedb27bfce2d2f01e91a3565380302bdee0f3b3204294672a5421daf
|
@@ -0,0 +1,109 @@
|
|
1
|
+
class MarkdownStreamFormatter
|
2
|
+
RESET = "\e[0m"
|
3
|
+
TITLE = "\e[33m"
|
4
|
+
CODE = "\e[32m"
|
5
|
+
BOLD = "\e[1m"
|
6
|
+
ITALIC = "\e[3m"
|
7
|
+
QUOTE = "\e[32m\e[1m"
|
8
|
+
|
9
|
+
def initialize
|
10
|
+
@line_buffer = ""
|
11
|
+
@title = false
|
12
|
+
@bold = false
|
13
|
+
@italic = false
|
14
|
+
@quote = false
|
15
|
+
@code = false
|
16
|
+
end
|
17
|
+
|
18
|
+
def next(chunk)
|
19
|
+
return unless chunk
|
20
|
+
|
21
|
+
chunk.lines.map do |line|
|
22
|
+
line = titleize(line)
|
23
|
+
line = boldify(line)
|
24
|
+
line = italicize(line)
|
25
|
+
line = quotify(line)
|
26
|
+
line = codify(line)
|
27
|
+
|
28
|
+
if line.end_with? "\n"
|
29
|
+
@title = false
|
30
|
+
@bold = false
|
31
|
+
@italic = false
|
32
|
+
@quote = false if @line_buffer.size == 0 && line == "\n"
|
33
|
+
@code = false
|
34
|
+
|
35
|
+
@line_buffer = ""
|
36
|
+
line += RESET
|
37
|
+
else
|
38
|
+
@line_buffer += line
|
39
|
+
end
|
40
|
+
|
41
|
+
line
|
42
|
+
end.join
|
43
|
+
end
|
44
|
+
|
45
|
+
private
|
46
|
+
|
47
|
+
def titleize(line)
|
48
|
+
if @line_buffer.empty? && line.match(/\A\s*[#]{1,5}/)
|
49
|
+
@title = true
|
50
|
+
|
51
|
+
"#{TITLE}#{line}"
|
52
|
+
end || line
|
53
|
+
end
|
54
|
+
|
55
|
+
def boldify(line)
|
56
|
+
while line.include? "**"
|
57
|
+
line.sub! "**", @bold ? RESET + current_ansi(bold: !@bold) : BOLD
|
58
|
+
|
59
|
+
@bold = !@bold
|
60
|
+
end
|
61
|
+
|
62
|
+
line
|
63
|
+
end
|
64
|
+
|
65
|
+
def italicize(line)
|
66
|
+
while line.include? "*"
|
67
|
+
line.sub! "*", @italic ? RESET + current_ansi(italic: !@italic) : ITALIC
|
68
|
+
|
69
|
+
@italic = !@italic
|
70
|
+
end
|
71
|
+
|
72
|
+
line
|
73
|
+
end
|
74
|
+
|
75
|
+
def quotify(line)
|
76
|
+
if @quote && @line_buffer.size == 0 && line != "\n"
|
77
|
+
line = "#{QUOTE}┃ #{RESET}#{line}"
|
78
|
+
elsif @line_buffer.size == 0 && line.match(/\A\s*>/)
|
79
|
+
@quote = true
|
80
|
+
|
81
|
+
line.sub! ">", "#{QUOTE}┃ #{RESET}"
|
82
|
+
end
|
83
|
+
|
84
|
+
line
|
85
|
+
end
|
86
|
+
|
87
|
+
def codify(line)
|
88
|
+
line.gsub! "`" do |match|
|
89
|
+
new_sub = @code ? "`" + RESET + current_ansi(code: !@code) : CODE + "`"
|
90
|
+
|
91
|
+
@code = !@code
|
92
|
+
|
93
|
+
new_sub
|
94
|
+
end
|
95
|
+
|
96
|
+
line
|
97
|
+
end
|
98
|
+
|
99
|
+
def current_ansi(bold: nil, italic: nil, code: nil)
|
100
|
+
style = ""
|
101
|
+
|
102
|
+
style << TITLE if @title
|
103
|
+
style << BOLD if (bold.nil? ? @bold : bold)
|
104
|
+
style << ITALIC if (italic.nil? ? @italic : italic)
|
105
|
+
style << CODE if (code.nil? ? @code : code)
|
106
|
+
|
107
|
+
style
|
108
|
+
end
|
109
|
+
end
|
metadata
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: markdown_stream_formatter
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Philipp Schlesinger
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2024-05-15 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Markdown parsing for the CLI supporting string streams, allowing to format
|
14
|
+
before you know the full markdown
|
15
|
+
email:
|
16
|
+
- info@philcomm.dev
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- lib/markdown_stream_formatter.rb
|
22
|
+
homepage: ''
|
23
|
+
licenses:
|
24
|
+
- MIT
|
25
|
+
metadata: {}
|
26
|
+
post_install_message:
|
27
|
+
rdoc_options: []
|
28
|
+
require_paths:
|
29
|
+
- lib
|
30
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - ">="
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: 3.1.0
|
35
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0'
|
40
|
+
requirements: []
|
41
|
+
rubygems_version: 3.5.9
|
42
|
+
signing_key:
|
43
|
+
specification_version: 4
|
44
|
+
summary: String stream markdown parser
|
45
|
+
test_files: []
|