marble_markdown 0.1
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/marble.rb +165 -0
- metadata +47 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 0e054f641aae366f4b79f6fdbb2fdd5806ec3dce82b1218873887723c42f37c9
|
4
|
+
data.tar.gz: 878fc70ddeca19a7590f8d79879a47a4e565b20d1fca9e05b3b5dd77ac0419cb
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 63b99e4feab505fdd58ebb1634813ecc7ab8a84ca87916fcddde89a27c73ab74ea484bd9e6e0f6946f08ad7b952b28250723a42560e436978b0a77cde8443222
|
7
|
+
data.tar.gz: fd1d19b166fbf3abc153ff344c9cd8019404f337186455862a9c4cc8bfb55beb4692247df0b14626a1a015b5cb29fe9a82098c08ec9256a9c0cb1d9bd95b1c4a
|
data/lib/marble.rb
ADDED
@@ -0,0 +1,165 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
#
|
3
|
+
# Copyright (C) 2021 biqqles.
|
4
|
+
#
|
5
|
+
# This Source Code Form is subject to the terms of the Mozilla Public
|
6
|
+
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
7
|
+
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
8
|
+
|
9
|
+
# The Marble Markdown formatter.
|
10
|
+
module Marble
|
11
|
+
VERSION = '0.1'
|
12
|
+
|
13
|
+
# build a hash of characters that must be escaped and their escaped forms,
|
14
|
+
# and a regex to detect the former.
|
15
|
+
# not using Array#to_h to maintain compatibility below Ruby 2.6
|
16
|
+
ESCAPED = %w{! # ( ) * [ \\ ] _ ` | ~}.map { |c| [c, "\\#{c}"] }.to_h
|
17
|
+
TO_ESCAPE = Regexp.union(ESCAPED.keys)
|
18
|
+
|
19
|
+
module_function
|
20
|
+
|
21
|
+
# Return a string with all Markdown syntax escaped.
|
22
|
+
# @param text [String]
|
23
|
+
def escape(text)
|
24
|
+
text.gsub(TO_ESCAPE, ESCAPED)
|
25
|
+
end
|
26
|
+
|
27
|
+
# Create bolded Markdown text.
|
28
|
+
# @param text [String]
|
29
|
+
# @return [String]
|
30
|
+
def bold(text)
|
31
|
+
"**#{text}**"
|
32
|
+
end
|
33
|
+
|
34
|
+
# Create a code block. If `language` is not `nil`, the code block will be "fenced".
|
35
|
+
# If you want a fenced code block with no syntax highlighting, set `language` to an empty string.
|
36
|
+
# @param text [String]
|
37
|
+
# @param language [String, nil]
|
38
|
+
def code(text, language: nil)
|
39
|
+
if language.nil?
|
40
|
+
"`#{text}`"
|
41
|
+
else
|
42
|
+
"```#{language}\n#{text}\n```"
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
# Create italicised Markdown text.
|
47
|
+
# @param text [String]
|
48
|
+
# @return [String]
|
49
|
+
def italic(text)
|
50
|
+
"*#{text}*"
|
51
|
+
end
|
52
|
+
|
53
|
+
# Create struck-through Markdown text.
|
54
|
+
# @param text [String]
|
55
|
+
def strikethrough(text)
|
56
|
+
"~~#{text}~~"
|
57
|
+
end
|
58
|
+
|
59
|
+
# Create a block quote.
|
60
|
+
def quote(text)
|
61
|
+
"> #{text}"
|
62
|
+
end
|
63
|
+
|
64
|
+
# Create a Markdown link.
|
65
|
+
# @param text [String]
|
66
|
+
# @param url [String]
|
67
|
+
def link(text, url)
|
68
|
+
"[#{text}](#{url})"
|
69
|
+
end
|
70
|
+
|
71
|
+
# Create a Markdown image.
|
72
|
+
# @param alt_text [String]
|
73
|
+
# @param url [String]
|
74
|
+
def image(alt_text, url)
|
75
|
+
""
|
76
|
+
end
|
77
|
+
|
78
|
+
# Create an ordered list.
|
79
|
+
# @param items [Array<String>]
|
80
|
+
# @param start [Integer]
|
81
|
+
def ordered_list(items, start: 1)
|
82
|
+
items.each.with_index(start).map { |item, n| "#{n}. #{item}\n" }.join
|
83
|
+
end
|
84
|
+
|
85
|
+
# Create an unordered list.
|
86
|
+
# @param items [Enumerable<String>]
|
87
|
+
def unordered_list(items)
|
88
|
+
items.map { |item| "- #{item}\n" }.join
|
89
|
+
end
|
90
|
+
|
91
|
+
# Create a horizontal rule.
|
92
|
+
def horizontal_rule
|
93
|
+
'---'
|
94
|
+
end
|
95
|
+
|
96
|
+
# Create headers of various levels.
|
97
|
+
def h1(title)
|
98
|
+
"# #{title}"
|
99
|
+
end
|
100
|
+
|
101
|
+
def h2(title)
|
102
|
+
"## #{title}"
|
103
|
+
end
|
104
|
+
|
105
|
+
def h3(title)
|
106
|
+
"### #{title}"
|
107
|
+
end
|
108
|
+
|
109
|
+
def h4(title)
|
110
|
+
"#### #{title}"
|
111
|
+
end
|
112
|
+
|
113
|
+
def h5(title)
|
114
|
+
"##### #{title}"
|
115
|
+
end
|
116
|
+
|
117
|
+
def h6(title)
|
118
|
+
"###### #{title}"
|
119
|
+
end
|
120
|
+
|
121
|
+
# define aliases
|
122
|
+
class << self
|
123
|
+
alias hr horizontal_rule
|
124
|
+
alias ol ordered_list
|
125
|
+
alias ul unordered_list
|
126
|
+
alias italics italic
|
127
|
+
alias strike strikethrough
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
# The most common formatting operations are added to String as extension methods.
|
132
|
+
class String
|
133
|
+
# This string with all Markdown syntax escaped.
|
134
|
+
def escape
|
135
|
+
Marble.escape self
|
136
|
+
end
|
137
|
+
|
138
|
+
# Embolden this string.
|
139
|
+
def bold
|
140
|
+
Marble.bold self
|
141
|
+
end
|
142
|
+
|
143
|
+
# Display this string as code.
|
144
|
+
def code
|
145
|
+
Marble.code self
|
146
|
+
end
|
147
|
+
|
148
|
+
# Italicise this string.
|
149
|
+
def italic
|
150
|
+
Marble.italic self
|
151
|
+
end
|
152
|
+
|
153
|
+
# Strike-out this string.
|
154
|
+
def strikethrough
|
155
|
+
Marble.strikethrough self
|
156
|
+
end
|
157
|
+
|
158
|
+
# Create a link using this string as the text.
|
159
|
+
def link(url)
|
160
|
+
Marble.link self, url
|
161
|
+
end
|
162
|
+
|
163
|
+
alias italics italic
|
164
|
+
alias strike strikethrough
|
165
|
+
end
|
metadata
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: marble_markdown
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '0.1'
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- biqqles
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2021-04-06 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description:
|
14
|
+
email:
|
15
|
+
- biqqles@protonmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- lib/marble.rb
|
21
|
+
homepage: https://github.com/biqqles/marble
|
22
|
+
licenses:
|
23
|
+
- MPL-2.0
|
24
|
+
metadata:
|
25
|
+
homepage_uri: https://github.com/biqqles/marble
|
26
|
+
source_code_uri: https://github.com/biqqles/marble
|
27
|
+
changelog_uri: https://github.com/biqqles/marble/releases
|
28
|
+
post_install_message:
|
29
|
+
rdoc_options: []
|
30
|
+
require_paths:
|
31
|
+
- lib
|
32
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
33
|
+
requirements:
|
34
|
+
- - ">="
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: 2.4.0
|
37
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
requirements: []
|
43
|
+
rubygems_version: 3.1.2
|
44
|
+
signing_key:
|
45
|
+
specification_version: 4
|
46
|
+
summary: The Ruby Markdown formatter
|
47
|
+
test_files: []
|