raindown 0.18.0 → 0.21.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 +4 -4
- data/lib/raindown/area.rb +88 -0
- data/lib/raindown/chart/chart.rb +5 -2
- data/lib/raindown/chart/drawing.rb +0 -2
- data/lib/raindown/chart/line.rb +1 -1
- data/lib/raindown/chart/points.rb +2 -1
- data/lib/raindown/cli.rb +2 -2
- data/lib/raindown/code.rb +37 -5
- data/lib/raindown/config.rb +11 -0
- data/lib/raindown/document.rb +8 -6
- data/lib/raindown/editing.rb +8 -4
- data/lib/raindown/endpoint/curl.rb +19 -14
- data/lib/raindown/endpoint/curl_request.rb +90 -0
- data/lib/raindown/endpoint/curl_response.rb +6 -3
- data/lib/raindown/grid.rb +154 -0
- data/lib/raindown/grid_directives.rb +43 -0
- data/lib/raindown/grid_wrapping.rb +46 -0
- data/lib/raindown/http_json.rb +45 -0
- data/lib/raindown/image.rb +44 -0
- data/lib/raindown/js/js_nodes.rb +85 -33
- data/lib/raindown/js/quickjs_host.rb +63 -0
- data/lib/raindown/js/quickjs_runtime.js +61 -0
- data/lib/raindown/js/services.js +31 -0
- data/lib/raindown/markdown_nodes.rb +2 -0
- data/lib/raindown/node_blocks.rb +22 -9
- data/lib/raindown/node_injector.rb +56 -15
- data/lib/raindown/nodes.rb +52 -15
- data/lib/raindown/outdent.rb +100 -0
- data/lib/raindown/pages/listings.rb +71 -0
- data/lib/raindown/{pages.rb → pages/pages.rb} +21 -34
- data/lib/raindown/problem.rb +12 -3
- data/lib/raindown/project/card.rb +54 -0
- data/lib/raindown/project/endpoint.rb +14 -0
- data/lib/raindown/project/project.rb +168 -0
- data/lib/raindown/project_nodes.rb +47 -0
- data/lib/raindown/projects.rb +1 -1
- data/lib/raindown/rasters.rb +98 -0
- data/lib/raindown/scene.rb +10 -0
- data/lib/raindown/services/connections.rb +65 -0
- data/lib/raindown/services/local.rb +139 -0
- data/lib/raindown/services.rb +72 -0
- data/lib/raindown/skeleton.rb +1 -1
- data/lib/raindown/table/table.rb +2 -2
- data/lib/raindown/table/table_column.rb +0 -2
- data/lib/raindown/table/table_file.rb +2 -2
- data/lib/raindown/table/written_table.rb +0 -2
- data/lib/raindown/toc/markdown_headings.rb +98 -0
- data/lib/raindown/{toc.rb → toc/toc.rb} +8 -5
- data/lib/raindown/tui/endpoints.rb +1 -1
- data/lib/raindown/tui/feelings/feelings_log.rb +2 -1
- data/lib/raindown/tui/navigator/navigator.rb +1 -1
- data/lib/raindown/watcher/project.rb +2 -2
- data/lib/raindown/weather/branch_tree.rb +129 -0
- data/lib/raindown/weather/forecast.rb +193 -0
- data/lib/raindown/weather/forecast_cache.rb +58 -0
- data/lib/raindown/weather/forecast_cards.rb +115 -0
- data/lib/raindown/weather/sky.rb +82 -0
- data/lib/raindown/weather/weather.rb +140 -0
- data/lib/raindown/wiki_links.rb +2 -6
- data/lib/raindown.rb +39 -14
- data/lib/version.rb +1 -1
- metadata +33 -9
- data/lib/raindown/js/host.mjs +0 -51
- data/lib/raindown/tui/card.rb +0 -56
- data/lib/raindown/tui/endpoint.rb +0 -16
- data/lib/raindown/tui/project.rb +0 -147
- /data/lib/raindown/{page.rb → pages/page.rb} +0 -0
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 537d6e6dd373dffcd9616142d4cf5798f281c96d7aa4177fee5731d0060b2226
|
|
4
|
+
data.tar.gz: ec9e9426b6c9fc8b41ff9ba59eaff4fd76819cc15462ae3f0ee8f496a15a821d
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: ec35463daeacfaad63e3abddd53bf2a4061d5cafdab2a43a79c4afeb00f4c39df5c53d674b84bd5dc9a79f8dfe479976955bfca842a7dffb31e00bcc30f4a42e
|
|
7
|
+
data.tar.gz: 7a1dfeb15899ea565fcc430fcacc163c7a0d759af878dfb68fcc9371e408c0f2f31eeba8f8d50e5aa5b9fa84929c1fc0d6000a5cd6af906ab85ac9d049b8ee0b
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Raindown
|
|
4
|
+
# The space a Ruby node paints behind its text in an app that draws pixels, given to its rasterize as area:, like a
|
|
5
|
+
# JavaScript Component's, painted in Ruby: its width and height, in pixels, the time, where the text ends on each of
|
|
6
|
+
# its rows, in pixels across, a character's width and height, as cell, its pixels, each set as 0xRRGGBB, one at a time,
|
|
7
|
+
# like area.pixels[x, y] = 0x2e3440, or all at once, with area.pixels = colors, and the buttons it's painted, which,
|
|
8
|
+
# clicked, change the props in set on its node's directive. Its rasterize gives how many milliseconds until it's
|
|
9
|
+
# painted again, like a JavaScript one's, or nothing, for when its size or text changes, which an app makes it wait
|
|
10
|
+
# longer for when painting it takes long, as each time is a trip to Ruby and its pixels back. A pixel it doesn't set
|
|
11
|
+
# shows what's behind it. Its pixels are only made once it paints one, so a node that gives a Scene instead, like
|
|
12
|
+
# weather:, costs nothing.
|
|
13
|
+
class Area
|
|
14
|
+
attr_reader :width, :height, :time, :text_ends, :cell, :buttons
|
|
15
|
+
|
|
16
|
+
def initialize(width:, height:, text_ends: [], cell: [8, 16], time: Time.now)
|
|
17
|
+
@width = width
|
|
18
|
+
@height = height
|
|
19
|
+
@text_ends = text_ends
|
|
20
|
+
@cell = cell
|
|
21
|
+
@time = time
|
|
22
|
+
@buttons = []
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def pixels
|
|
26
|
+
@pixels ||= Pixels.new(@width, @height)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# All its pixels at once, as 0xRRGGBB, across then down, width by height.
|
|
30
|
+
def pixels=(colors)
|
|
31
|
+
@pixels = Pixels.new(@width, @height, colors)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# A rectangle it's painted a button, which, clicked, changes the props in set on its node's directive, like
|
|
35
|
+
# { zoom: 8 }, as typing them there would, and only props the directive already has.
|
|
36
|
+
def button(x:, y:, width:, height:, set:)
|
|
37
|
+
@buttons << { x:, y:, width:, height:, set: }
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# Its pixels as bytes, red, green, blue and alpha for each, those it didn't set clear.
|
|
41
|
+
def to_bytes
|
|
42
|
+
return "\0".b * (@width * @height * 4) unless @pixels
|
|
43
|
+
|
|
44
|
+
@pixels.to_bytes
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
# For an app, its pixels' bytes in Base64, to copy in as they are, its buttons, and how many milliseconds until
|
|
48
|
+
# it's painted again, if it said.
|
|
49
|
+
def to_h(again = nil)
|
|
50
|
+
{ painter: 'pixels', width:, height:, pixels: [to_bytes].pack('m0'), buttons:, again: }
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
# An area's pixels, each a colour as 0xRRGGBB, or nil where it's clear.
|
|
54
|
+
class Pixels
|
|
55
|
+
def initialize(width, height, colors = [])
|
|
56
|
+
@width = width
|
|
57
|
+
@height = height
|
|
58
|
+
@colors = Array.new(width * height) { colors[it] }
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def [](x, y)
|
|
62
|
+
@colors[(y.round * @width) + x.round] if inside?(x.round, y.round)
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
# A pixel, at whole pixels, so a node can give any point, as a pixel between two isn't one, and none outside it.
|
|
66
|
+
def []=(x, y, color)
|
|
67
|
+
x = x.round
|
|
68
|
+
y = y.round
|
|
69
|
+
@colors[(y * @width) + x] = color if inside?(x, y)
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def to_bytes
|
|
73
|
+
@colors.map { byte_of(it) }.pack('N*')
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
private
|
|
77
|
+
|
|
78
|
+
def inside?(x, y) = x >= 0 && y >= 0 && x < @width && y < @height
|
|
79
|
+
|
|
80
|
+
# A pixel's colour with its alpha, opaque, or clear when it wasn't set.
|
|
81
|
+
def byte_of(color)
|
|
82
|
+
return 0 if color.nil?
|
|
83
|
+
|
|
84
|
+
(color << 8) | 0xff
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
end
|
data/lib/raindown/chart/chart.rb
CHANGED
|
@@ -31,9 +31,12 @@ module Raindown
|
|
|
31
31
|
@tail = tail
|
|
32
32
|
end
|
|
33
33
|
|
|
34
|
-
# The data is read every time, so the chart follows changes to its file.
|
|
34
|
+
# The data is read every time, so the chart follows changes to its file. A chart with points of its own, like a
|
|
35
|
+
# project's chart of its commits, needs no file.
|
|
35
36
|
def rewrite(metadata: NoMetadata)
|
|
36
|
-
|
|
37
|
+
if ['', nil].include?(@data) && method(:points).owner == Chart
|
|
38
|
+
return Problem.alert('Needs a file path to a CSV, TSV, JSON or YAML file')
|
|
39
|
+
end
|
|
37
40
|
|
|
38
41
|
points = points(metadata:)
|
|
39
42
|
return '' if points.empty?
|
data/lib/raindown/chart/line.rb
CHANGED
data/lib/raindown/cli.rb
CHANGED
|
@@ -166,7 +166,7 @@ module Raindown
|
|
|
166
166
|
def render(path, output = nil, bare: false, execute: false)
|
|
167
167
|
ensure_exists(path)
|
|
168
168
|
abort('--html renders a file, not a directory') if File.directory?(path)
|
|
169
|
-
Nodes.load(path)
|
|
169
|
+
warn(*Nodes.load(path).map { "raindown: #{it}" })
|
|
170
170
|
|
|
171
171
|
metadata = Pages.for(path)
|
|
172
172
|
rewrite = Rewriter.update(path:, bare:, execute:, metadata:)
|
|
@@ -200,7 +200,7 @@ module Raindown
|
|
|
200
200
|
ensure_exists(path)
|
|
201
201
|
return Watcher::Supervisor.new(directory: path, bare:, execute:).start if File.directory?(path)
|
|
202
202
|
|
|
203
|
-
Nodes.load(path)
|
|
203
|
+
warn(*Nodes.load(path).map { "raindown: #{it}" })
|
|
204
204
|
Watcher::FileWatcher.new(path:, bare:, execute:).start
|
|
205
205
|
end
|
|
206
206
|
|
data/lib/raindown/code.rb
CHANGED
|
@@ -1,24 +1,56 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
require_relative 'code_blocks'
|
|
4
4
|
|
|
5
5
|
module Raindown
|
|
6
6
|
# Finds code in Markdown the way Commonmarker reads it: code spans and fenced, indented or quoted code blocks.
|
|
7
7
|
module Code
|
|
8
|
+
# A code span on one line, like `{x}`, between runs of the same number of backticks.
|
|
9
|
+
SPAN = /(?<!`)(`+)(?!`).+?(?<!`)\1(?!`)/n
|
|
10
|
+
|
|
8
11
|
module_function
|
|
9
12
|
|
|
10
|
-
# Byte ranges of code in the Markdown, from Commonmarker's line and column positions
|
|
13
|
+
# Byte ranges of code in the Markdown, from Commonmarker's line and column positions, or rendering Markdown, which
|
|
14
|
+
# doesn't load Commonmarker, its fenced blocks and code spans.
|
|
11
15
|
def ranges(markdown)
|
|
12
|
-
|
|
16
|
+
return scanned_ranges(markdown) unless Raindown.commonmark?
|
|
17
|
+
|
|
18
|
+
offsets = line_offsets(markdown.lines)
|
|
13
19
|
ranges = []
|
|
14
20
|
|
|
15
|
-
|
|
16
|
-
ranges << range(node.source_position,
|
|
21
|
+
Raindown.parse_markdown(markdown).walk do |node|
|
|
22
|
+
ranges << range(node.source_position, offsets) if %i[code code_block].include?(node.type)
|
|
17
23
|
end
|
|
18
24
|
|
|
19
25
|
ranges
|
|
20
26
|
end
|
|
21
27
|
|
|
28
|
+
# The byte offset each line starts at, and the end's.
|
|
29
|
+
def line_offsets(lines)
|
|
30
|
+
lines.reduce([0]) { |offsets, line| offsets << (offsets.last + line.bytesize) }
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def scanned_ranges(markdown)
|
|
34
|
+
lines = markdown.lines
|
|
35
|
+
offsets = line_offsets(lines)
|
|
36
|
+
ranges = []
|
|
37
|
+
index = 0
|
|
38
|
+
while index < lines.length
|
|
39
|
+
if (block = CodeBlocks.at(lines, index))
|
|
40
|
+
ranges << (offsets[index]...offsets[block[:close] + 1])
|
|
41
|
+
index = block[:close]
|
|
42
|
+
else
|
|
43
|
+
ranges.concat(span_ranges(lines[index], offsets[index]))
|
|
44
|
+
end
|
|
45
|
+
index += 1
|
|
46
|
+
end
|
|
47
|
+
ranges
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def span_ranges(line, start)
|
|
51
|
+
line.b.to_enum(:scan, SPAN).map { (start + ::Regexp.last_match.begin(0))...(start + ::Regexp.last_match.end(0)) }
|
|
52
|
+
end
|
|
53
|
+
|
|
22
54
|
def range(position, line_offsets)
|
|
23
55
|
start = line_offsets[position[:start_line] - 1] + position[:start_column] - 1
|
|
24
56
|
# A block that ends with its last line ends at column 0 of the line after.
|
data/lib/raindown/config.rb
CHANGED
|
@@ -5,9 +5,11 @@ require 'yaml'
|
|
|
5
5
|
module Raindown
|
|
6
6
|
# A project's settings, from config.yml in its .raindown folder:
|
|
7
7
|
# highlight_code: false # Code blocks aren't highlighted, so the browser can highlight them instead.
|
|
8
|
+
# kind: notes # What the project's for, code or notes, so an app shows what fits it.
|
|
8
9
|
module Config
|
|
9
10
|
FILE = 'config.yml'
|
|
10
11
|
DEFAULTS = { highlight_code: true }.freeze
|
|
12
|
+
KINDS = %w[code notes].freeze
|
|
11
13
|
|
|
12
14
|
module_function
|
|
13
15
|
|
|
@@ -20,6 +22,15 @@ module Raindown
|
|
|
20
22
|
DEFAULTS.merge(YAML.safe_load_file(file, symbolize_names: true) || {})
|
|
21
23
|
end
|
|
22
24
|
|
|
25
|
+
# The project's kind, code or notes, from kind: in its config, or else code when it's a git repository.
|
|
26
|
+
def kind(root)
|
|
27
|
+
kind = self.for(File.join(root, FILE))[:kind].to_s
|
|
28
|
+
return kind if KINDS.include?(kind)
|
|
29
|
+
return 'code' if File.exist?(File.join(root, '.git'))
|
|
30
|
+
|
|
31
|
+
'notes'
|
|
32
|
+
end
|
|
33
|
+
|
|
23
34
|
# The settings for rendering a file, like Raindown.render(**Config.render_options(path)).
|
|
24
35
|
def render_options(path)
|
|
25
36
|
self.for(path).slice(:highlight_code)
|
data/lib/raindown/document.rb
CHANGED
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require 'commonmarker'
|
|
4
|
-
|
|
5
3
|
require_relative '../raindown'
|
|
6
4
|
require_relative 'bare_directives'
|
|
7
5
|
require_relative 'code_blocks'
|
|
8
6
|
require_relative 'endpoint/endpoint_runner'
|
|
9
7
|
require_relative 'front_matter'
|
|
10
8
|
require_relative 'node_blocks'
|
|
9
|
+
require_relative 'outdent'
|
|
11
10
|
|
|
12
11
|
module Raindown
|
|
13
12
|
# A Markdown file's structure, read from its source so each part has the line it's on, for tools like an editor.
|
|
@@ -47,7 +46,9 @@ module Raindown
|
|
|
47
46
|
|
|
48
47
|
def initialize(markdown:, bare: false)
|
|
49
48
|
@markdown = markdown
|
|
50
|
-
|
|
49
|
+
# An outdent block's lines are read without their <, so its directives are found as anywhere else.
|
|
50
|
+
@lines = Outdent.without_markers(markdown).lines
|
|
51
|
+
@outdents = Outdent.blocks(markdown.lines)
|
|
51
52
|
@bare = bare || BareDirectives.front_matter?(markdown)
|
|
52
53
|
end
|
|
53
54
|
|
|
@@ -72,7 +73,7 @@ module Raindown
|
|
|
72
73
|
private
|
|
73
74
|
|
|
74
75
|
def root
|
|
75
|
-
@root ||=
|
|
76
|
+
@root ||= Raindown.parse_markdown(@markdown)
|
|
76
77
|
end
|
|
77
78
|
|
|
78
79
|
def text(heading)
|
|
@@ -86,8 +87,9 @@ module Raindown
|
|
|
86
87
|
|
|
87
88
|
def node_directives
|
|
88
89
|
NodeBlocks.spans(@lines, bare: @bare).map do |span|
|
|
89
|
-
|
|
90
|
-
|
|
90
|
+
last = span[:close] ? span[:last] : Outdent.bare_block_last(@outdents, span[:open], span[:last])
|
|
91
|
+
output = trimmed(span[:first], span[:close] ? span[:close] - 1 : last)
|
|
92
|
+
directive(span[:name], span[:args], span[:open]..last, output:, writes: span[:writes])
|
|
91
93
|
end
|
|
92
94
|
end
|
|
93
95
|
|
data/lib/raindown/editing.rb
CHANGED
|
@@ -10,15 +10,19 @@ module Raindown
|
|
|
10
10
|
module Editing
|
|
11
11
|
module_function
|
|
12
12
|
|
|
13
|
-
|
|
13
|
+
# Without commonmark, it's Markdown, for the editor to render itself.
|
|
14
|
+
def render_markdown(markdown, path:, bare: false, commonmark: true)
|
|
14
15
|
Nodes.load(path)
|
|
15
|
-
Raindown.render(markdown:, metadata: Pages.for(path), bare: bare || BareDirectives.file?(path),
|
|
16
|
+
Raindown.render(markdown:, metadata: Pages.for(path), bare: bare || BareDirectives.file?(path), commonmark:,
|
|
17
|
+
**Config.render_options(path))
|
|
16
18
|
end
|
|
17
19
|
|
|
20
|
+
# Its warnings say why any of the project's nodes couldn't be loaded too.
|
|
18
21
|
def rewrite_markdown(markdown, path:, bare: false, execute: false)
|
|
19
|
-
Nodes.load(path)
|
|
22
|
+
load_errors = Nodes.load(path)
|
|
20
23
|
rewrite = Rewriter.rewrite(content: markdown, path:, bare:, execute:, metadata: Pages.for(path))
|
|
21
|
-
|
|
24
|
+
failures = rewrite.failures.map { "Endpoint check failed: #{it}" }
|
|
25
|
+
{ output: rewrite.content, warnings: load_errors + failures + rewrite.problems }
|
|
22
26
|
end
|
|
23
27
|
end
|
|
24
28
|
end
|
|
@@ -1,10 +1,8 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require 'ipaddr'
|
|
4
3
|
require 'open3'
|
|
5
4
|
require 'shellwords'
|
|
6
|
-
|
|
7
|
-
require 'uri'
|
|
5
|
+
require_relative 'curl_request'
|
|
8
6
|
require_relative 'curl_response'
|
|
9
7
|
|
|
10
8
|
module Raindown
|
|
@@ -13,9 +11,14 @@ module Raindown
|
|
|
13
11
|
# are allowed: nothing that reads or writes local files, loads a config, or uses another protocol.
|
|
14
12
|
# Link-local addresses, like the cloud metadata service at 169.254.169.254, are refused, as they can give out
|
|
15
13
|
# credentials, and redirects aren't followed (no -L), so a URL can't redirect there either.
|
|
14
|
+
# Or, without processes, like in WebAssembly, the request's sent by Curl.fetcher, as the Obsidian plugin sends it with
|
|
15
|
+
# Obsidian's requestUrl, which refuses link-local hosts but can't pin an address or stop redirects as curl does here.
|
|
16
16
|
module Curl
|
|
17
17
|
class Error < StandardError; end
|
|
18
|
-
|
|
18
|
+
class << self
|
|
19
|
+
# Given a Request, returns its Response, in place of curl.
|
|
20
|
+
attr_accessor :fetcher
|
|
21
|
+
end
|
|
19
22
|
|
|
20
23
|
TIMEOUT = '30'
|
|
21
24
|
# The variables curl reads a proxy from, which then looks the names up rather than curl.
|
|
@@ -54,6 +57,8 @@ module Raindown
|
|
|
54
57
|
# Placeholders like {base_url} are replaced from the variables, word by word, so a value can't add options.
|
|
55
58
|
def run(snippet, variables = nil)
|
|
56
59
|
args = arguments(snippet, variables)
|
|
60
|
+
return fetch(args) if fetcher
|
|
61
|
+
|
|
57
62
|
stdout, stderr, status = Open3.capture3('curl', *SAFE_OPTIONS, '--silent', '--show-error', '--max-time', TIMEOUT,
|
|
58
63
|
*resolve_options(args), *args, '--write-out', Response::WRITE_OUT)
|
|
59
64
|
raise Error, stderr.strip.empty? ? "curl exited with #{status.exitstatus}" : stderr.strip unless status.success?
|
|
@@ -63,22 +68,19 @@ module Raindown
|
|
|
63
68
|
raise Error, 'curl is not installed'
|
|
64
69
|
end
|
|
65
70
|
|
|
66
|
-
# The snippet's
|
|
71
|
+
# The snippet's request, like GET https://httpbin.org/uuid, or nil when it can't be read.
|
|
67
72
|
def request(snippet, variables = nil)
|
|
68
|
-
|
|
69
|
-
Request.new(method: request_method(args), url: args.find { it.match?(URL) })
|
|
73
|
+
Request.from(arguments(snippet, variables))
|
|
70
74
|
rescue Error
|
|
71
75
|
nil
|
|
72
76
|
end
|
|
73
77
|
|
|
74
|
-
#
|
|
75
|
-
def
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
return 'HEAD' if args.include?('--head')
|
|
79
|
-
return 'POST' if args.intersect?(VALUE_CHECKS.keys.grep(/data|json/))
|
|
78
|
+
# With --fail, an error status fails, as curl does.
|
|
79
|
+
def fetch(args)
|
|
80
|
+
response = fetcher.call(Request.from(args))
|
|
81
|
+
raise Error, "The requested URL returned error: #{response.status}" if args.include?('--fail') && response.status >= 400
|
|
80
82
|
|
|
81
|
-
|
|
83
|
+
response
|
|
82
84
|
end
|
|
83
85
|
|
|
84
86
|
def arguments(snippet, variables = nil)
|
|
@@ -162,7 +164,10 @@ module Raindown
|
|
|
162
164
|
# Resolves each URL's host and pins curl to the address it was checked at, like --resolve api.io:443:203.0.113.5,
|
|
163
165
|
# so the name can't point somewhere else by the time curl looks it up. A host that's already an IP isn't pinned,
|
|
164
166
|
# and behind a proxy, which looks names up itself, only a host that's an IP is checked.
|
|
167
|
+
# Sockets are loaded only when an endpoint runs, so writing a file back loads none, as WebAssembly has none.
|
|
165
168
|
def resolve_options(args)
|
|
169
|
+
require 'ipaddr'
|
|
170
|
+
require 'socket'
|
|
166
171
|
args.grep(URL).flat_map { pin_option(URI.parse(it)) }
|
|
167
172
|
rescue URI::InvalidURIError => e
|
|
168
173
|
raise Error, "Can't read the URL: #{e.message}"
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Raindown
|
|
4
|
+
module Curl
|
|
5
|
+
# The request a snippet makes, with its headers and body, as curl would send it, for Curl.fetcher to send in its place.
|
|
6
|
+
# Data, joined by &, is the body, as a form unless it's --json, or with --get, the URL's query.
|
|
7
|
+
class Request < Data.define(:method, :url, :headers, :body)
|
|
8
|
+
FORM = { 'Content-Type' => 'application/x-www-form-urlencoded' }.freeze
|
|
9
|
+
JSON_HEADERS = { 'Content-Type' => 'application/json', 'Accept' => 'application/json' }.freeze
|
|
10
|
+
HEADER_OPTIONS = { '--user-agent' => 'User-Agent', '--referer' => 'Referer' }.freeze
|
|
11
|
+
|
|
12
|
+
def initialize(method:, url:, headers: {}, body: nil) = super
|
|
13
|
+
|
|
14
|
+
# From curl's checked arguments, with their options spelled long.
|
|
15
|
+
def self.from(args)
|
|
16
|
+
options = options(args)
|
|
17
|
+
url = options.assoc('--url').last
|
|
18
|
+
data = options.filter_map { |option, value| data_value(option, value) }.join('&')
|
|
19
|
+
headers = headers(options)
|
|
20
|
+
return new(method: request_method(args), url:, headers:) if data.empty?
|
|
21
|
+
return new(method: request_method(args), url: with_query(url, data), headers:) if args.include?('--get')
|
|
22
|
+
|
|
23
|
+
new(method: request_method(args), url:, headers: FORM.merge(headers), body: data)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
# A snippet that sends data is a POST, unless it says otherwise, as with curl.
|
|
27
|
+
def self.request_method(args)
|
|
28
|
+
index = args.index('--request')
|
|
29
|
+
return args[index + 1].upcase if index
|
|
30
|
+
return 'HEAD' if args.include?('--head')
|
|
31
|
+
return 'GET' if args.include?('--get')
|
|
32
|
+
return 'POST' if args.intersect?(VALUE_CHECKS.keys.grep(/data|json/))
|
|
33
|
+
|
|
34
|
+
'GET'
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# The options, each with its value or nil, and each URL as a --url, like ['--url', 'https://a.io'].
|
|
38
|
+
def self.options(args)
|
|
39
|
+
words = args.dup
|
|
40
|
+
pairs = []
|
|
41
|
+
until words.empty?
|
|
42
|
+
word = words.shift
|
|
43
|
+
pairs << if VALUE_CHECKS.key?(word) then [word, words.shift]
|
|
44
|
+
elsif word.start_with?('--') then [word, nil]
|
|
45
|
+
else ['--url', word]
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
pairs
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def self.headers(options)
|
|
52
|
+
options.each_with_object({}) do |(option, value), headers|
|
|
53
|
+
case option
|
|
54
|
+
when '--header'
|
|
55
|
+
name, header = value.split(':', 2)
|
|
56
|
+
headers[name.strip] = header.to_s.strip
|
|
57
|
+
when '--user' then headers['Authorization'] = "Basic #{[value].pack('m0')}"
|
|
58
|
+
when '--json' then headers.merge!(JSON_HEADERS)
|
|
59
|
+
else headers[HEADER_OPTIONS[option]] = value if HEADER_OPTIONS.key?(option)
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
# As curl sends it: --data without line breaks, and --data-urlencode encoded.
|
|
65
|
+
def self.data_value(option, value)
|
|
66
|
+
case option
|
|
67
|
+
when '--data', '--data-ascii' then value.delete("\r\n")
|
|
68
|
+
when '--data-raw', '--data-binary', '--json' then value
|
|
69
|
+
when '--data-urlencode' then urlencoded(value)
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
# Like curl's --data-urlencode, with name=content or =content, only the content's encoded.
|
|
74
|
+
def self.urlencoded(value)
|
|
75
|
+
name, equals, content = value.partition('=')
|
|
76
|
+
return URI.encode_uri_component(value) if equals.empty?
|
|
77
|
+
|
|
78
|
+
[name, URI.encode_uri_component(content)].reject(&:empty?).join('=')
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def self.with_query(url, query)
|
|
82
|
+
return "#{url}&#{query}" if url.include?('?')
|
|
83
|
+
|
|
84
|
+
"#{url}?#{query}"
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
private_class_method :options, :headers, :data_value, :urlencoded, :with_query
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
end
|
|
@@ -19,10 +19,13 @@ module Raindown
|
|
|
19
19
|
def self.parse(stdout)
|
|
20
20
|
body, _, info = stdout.rpartition(INFO_MARKER)
|
|
21
21
|
status, seconds, content_type = info.split(' ', 3)
|
|
22
|
-
|
|
22
|
+
received(body:, content_type:, status: status.to_i, milliseconds: (seconds.to_f * 1000).round)
|
|
23
|
+
end
|
|
23
24
|
|
|
24
|
-
|
|
25
|
-
|
|
25
|
+
# A response however it was sent, like by Curl.fetcher.
|
|
26
|
+
def self.received(body:, content_type:, status:, milliseconds:)
|
|
27
|
+
language = LANGUAGES.find { |pattern, _| content_type.to_s.match?(pattern) }&.last || ''
|
|
28
|
+
new(body: language == 'json' ? pretty_json(body) : body, language:, status:, milliseconds:)
|
|
26
29
|
end
|
|
27
30
|
|
|
28
31
|
def self.pretty_json(body)
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'node'
|
|
4
|
+
require_relative 'grid_directives'
|
|
5
|
+
require_relative 'grid_wrapping'
|
|
6
|
+
|
|
7
|
+
module Raindown
|
|
8
|
+
# The node behind grid:, a frame of cells in text, like a page of photos, which editors that draw images in text fill
|
|
9
|
+
# with the image in each cell:
|
|
10
|
+
# grid:
|
|
11
|
+
# +-------------------+------------------+
|
|
12
|
+
# |  |  |
|
|
13
|
+
# | | |
|
|
14
|
+
# +-------------------+------------------+
|
|
15
|
+
# It's written from itself, as it's edited: each | added to a row's line adds a cell to that row, each line added
|
|
16
|
+
# between its rules makes the row taller, each - added to or taken from a rule widens or narrows the grid, and each
|
|
17
|
+
# row's cells share its width, in characters, equally, a cell's text wrapping onto the row's next lines. With
|
|
18
|
+
# cell: :fit, a cell whose text needs more grows into the room the others don't need instead, and widens the grid when
|
|
19
|
+
# there's none. A new grid, with no rules yet, is 80 wide. With width:, it's always width: wide, and a row whose cells
|
|
20
|
+
# need more than that, with cell: :fit, shares it equally, its text wrapping. A cell that starts with a node's
|
|
21
|
+
# directive, like chart: or a project's component, runs it, as GridDirectives does.
|
|
22
|
+
class Grid < Node
|
|
23
|
+
RULE = /\A\s*\+[-+]*\+?\s*\z/
|
|
24
|
+
# A reference definition, like [cat]: pictures/cat.png, which an image like ![cat] in a cell takes its path from.
|
|
25
|
+
DEFINITION = /\A {0,3}\[[^\]]+\]:\s*\S/
|
|
26
|
+
NEW_GRID = [Array.new(3) { ['', ''] }].freeze
|
|
27
|
+
NEW_WIDTH = 80
|
|
28
|
+
|
|
29
|
+
def initialize(width: nil, cell: nil)
|
|
30
|
+
@width = width&.to_i
|
|
31
|
+
@fit = cell.to_s.delete_prefix(':') == 'fit'
|
|
32
|
+
super()
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def rewrite(written: [], metadata: NoMetadata)
|
|
36
|
+
rows = rows_run(written, metadata)
|
|
37
|
+
widths = widths_of(rows, written)
|
|
38
|
+
lines = [rule(widths.first)]
|
|
39
|
+
rows.each_with_index do |row, index|
|
|
40
|
+
wrapped(row, widths[index]).each { |cells| lines << row_line(cells, widths[index]) }
|
|
41
|
+
lines << rule(widths[index], widths[index + 1])
|
|
42
|
+
end
|
|
43
|
+
lines.join("\n")
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# Each row as a grid of its cells, sharing its width as the text does, its cells' Markdown rendered, with the note's
|
|
47
|
+
# reference definitions, so a cell's image can be by reference, like ![cat], its path on a line of its own below the
|
|
48
|
+
# grid, like [cat]: cat.png.
|
|
49
|
+
def render(written: [], document: '')
|
|
50
|
+
definitions = document.lines.grep(DEFINITION).join
|
|
51
|
+
rows = rows_of(written)
|
|
52
|
+
rows.zip(widths_of(rows, written)).map { |row, widths| row_html(row, widths, definitions) }.join
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
private
|
|
56
|
+
|
|
57
|
+
# Its rows, with their cells' directives run.
|
|
58
|
+
def rows_run(written, metadata) = rows_of(written).map { GridDirectives.run(it, metadata:) }
|
|
59
|
+
|
|
60
|
+
# The rows between its rules, each line of each as its cells' text, or a new grid when it has none.
|
|
61
|
+
def rows_of(written)
|
|
62
|
+
lines = written.map(&:chomp).reject { it.strip.empty? }
|
|
63
|
+
rows = lines.chunk_while { |line, following| !RULE.match?(line) && !RULE.match?(following) }
|
|
64
|
+
.reject { RULE.match?(it.first) }
|
|
65
|
+
.map { |row| row.map { cells_of(it) } }
|
|
66
|
+
rows.empty? ? NEW_GRID : rows
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def cells_of(line)
|
|
70
|
+
inner = line.strip.delete_prefix('|').delete_suffix('|')
|
|
71
|
+
inner.split('|', -1).map(&:strip)
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
# Its width, from its rules: the one edited to be longer or shorter than the lines left as they were, or as wide as
|
|
75
|
+
# those lines are, or a new grid's when it has no rules yet. Typing in a cell changes its row's line but never a rule.
|
|
76
|
+
def width_of(written)
|
|
77
|
+
lines = written.map(&:strip).reject(&:empty?)
|
|
78
|
+
rules = lines.grep(RULE).map(&:size)
|
|
79
|
+
return NEW_WIDTH if rules.empty?
|
|
80
|
+
|
|
81
|
+
kept = lines.map { Unicode::DisplayWidth.of(it, emoji: :all) }.tally.max_by(&:last).first
|
|
82
|
+
rules.find { it != kept } || kept
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
# Each row's cells' widths, inside their borders, as wide as width:, or as the grid, or as the widest row's cells need.
|
|
86
|
+
def widths_of(rows, written)
|
|
87
|
+
needs = rows.map { needs_of(it) }
|
|
88
|
+
inner = @width || [width_of(written), *needs.map { it.sum + it.size + 1 }].max
|
|
89
|
+
needs.map { |row| cell_widths(row, inner - row.size - 1) }
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
# The width each of a row's cells needs, inside its borders, for its text and a space either side of it, as it
|
|
93
|
+
# shows, so a wide character, like an emoji, takes the two it does, or none, unless cells fit their text.
|
|
94
|
+
def needs_of(row)
|
|
95
|
+
Array.new(row.map(&:size).max) do |column|
|
|
96
|
+
next 0 unless @fit
|
|
97
|
+
|
|
98
|
+
row.map { Unicode::DisplayWidth.of(" #{it[column]} ", emoji: :all) }.max
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
# The widths of a row's cells, inside their borders, sharing its room equally, but for those whose text needs more,
|
|
103
|
+
# which take what it needs, unless all their text needs more than the room.
|
|
104
|
+
def cell_widths(needs, room, grown = [])
|
|
105
|
+
shared = needs.each_index.to_a - grown
|
|
106
|
+
shares = shares_of(shared, room - needs.values_at(*grown).sum)
|
|
107
|
+
more = shared.select { needs[it] > shares[it] }
|
|
108
|
+
return cell_widths(needs, room, grown + more) if more.any? && needs.sum <= room
|
|
109
|
+
|
|
110
|
+
needs.each_index.map { shares[it] || needs[it] }
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
# The row with its cells' text wrapped to their widths, unless they fit it.
|
|
114
|
+
def wrapped(row, widths)
|
|
115
|
+
return row if @fit && needs_of(row).zip(widths).all? { |need, width| need <= width }
|
|
116
|
+
|
|
117
|
+
GridWrapping.wrapped(row, widths, kept: GridDirectives.columns(row))
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
# Room shared equally among cells, by their indexes, the first a character wider when it doesn't share evenly.
|
|
121
|
+
def shares_of(indexes, room)
|
|
122
|
+
base, extra = [room, indexes.size].max.divmod([indexes.size, 1].max)
|
|
123
|
+
indexes.each_with_index.to_h { |index, at| [index, base + (at < extra ? 1 : 0)] }
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
def row_html(row, widths, definitions)
|
|
127
|
+
cells = widths.each_index.map { |column| row.map { it[column].to_s }.join("\n").strip }
|
|
128
|
+
html_cells = cells.map { "<div>#{Raindown.commonmark("#{it}\n\n#{definitions}")}</div>" }.join
|
|
129
|
+
columns = widths.map { "minmax(0, #{it}fr)" }.join(' ')
|
|
130
|
+
%(<div class="raindown-grid" style="display: grid; grid-template-columns: #{columns}">#{html_cells}</div>)
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
# Each cell padded to its width as it shows, so a wide character, like an emoji, takes the two it does.
|
|
134
|
+
def row_line(cells, widths)
|
|
135
|
+
padded = widths.each_with_index.map do |width, index|
|
|
136
|
+
text = " #{cells[index]}"
|
|
137
|
+
text + (' ' * [width - Unicode::DisplayWidth.of(text, emoji: :all), 0].max)
|
|
138
|
+
end
|
|
139
|
+
"|#{padded.join('|')}|"
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
# A rule, with a + at each edge of the cells of the rows above and below it.
|
|
143
|
+
def rule(above, below = nil)
|
|
144
|
+
edges = [above, below].compact.flat_map { edges_of(it) }.uniq
|
|
145
|
+
line = '-' * (edges.max + 1)
|
|
146
|
+
edges.each { line[it] = '+' }
|
|
147
|
+
line
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
def edges_of(widths)
|
|
151
|
+
widths.reduce([0]) { |edges, width| edges << (edges.last + width + 1) }
|
|
152
|
+
end
|
|
153
|
+
end
|
|
154
|
+
end
|