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
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative '../actions'
|
|
4
|
+
require_relative '../bare_directives'
|
|
5
|
+
require_relative '../chart/chart'
|
|
6
|
+
require_relative '../document'
|
|
7
|
+
require_relative '../editing'
|
|
8
|
+
require_relative '../front_matter'
|
|
9
|
+
require_relative '../node_blocks'
|
|
10
|
+
require_relative '../nodes'
|
|
11
|
+
require_relative '../pages/pages'
|
|
12
|
+
require_relative '../projects'
|
|
13
|
+
require_relative '../project_nodes'
|
|
14
|
+
require_relative '../rasters'
|
|
15
|
+
require_relative '../workers/workers'
|
|
16
|
+
require_relative '../stat'
|
|
17
|
+
require_relative 'card'
|
|
18
|
+
require_relative 'endpoint'
|
|
19
|
+
require_relative '../code_blocks'
|
|
20
|
+
require_relative '../endpoint/curl'
|
|
21
|
+
require_relative '../endpoint/endpoint_variables'
|
|
22
|
+
|
|
23
|
+
module Raindown
|
|
24
|
+
# What a UI, like the terminal UI or the app, asks of a project, answered in the project's Worker with its own nodes loaded,
|
|
25
|
+
# so two projects' nodes of the same name stay apart. Answers are plain data, which a Worker sends back through a pipe.
|
|
26
|
+
class Project
|
|
27
|
+
Outline = Data.define(:lines, :headings, :directives)
|
|
28
|
+
|
|
29
|
+
attr_reader :root
|
|
30
|
+
|
|
31
|
+
# Workers for the projects in the folder, each answering as a Project, which allows bare directives in any file,
|
|
32
|
+
# and runs endpoints with no response as it writes back, when asked, like with --bare and -x.
|
|
33
|
+
def self.workers(root:, bare: false, execute: false, **)
|
|
34
|
+
Workers.new(root:, **) { new(root: it, bare:, execute:) }
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# The projects inside this one have workers of their own, so their files are left out.
|
|
38
|
+
def initialize(root:, bare: false, execute: false)
|
|
39
|
+
@root = root
|
|
40
|
+
@bare = bare
|
|
41
|
+
@execute = execute
|
|
42
|
+
@file_errors = []
|
|
43
|
+
@load_errors = Nodes.load(File.join(root, Nodes::FOLDER))
|
|
44
|
+
rescue StandardError, ScriptError => e
|
|
45
|
+
@load_errors = ["#{File.basename(root)}/#{Nodes::FOLDER}: #{e.message}"]
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# A file's lines, headings and directives.
|
|
49
|
+
def outline(path)
|
|
50
|
+
document = document(path)
|
|
51
|
+
Outline.new(lines: File.readlines(path, chomp: true), headings: document.headings, directives: document.directives)
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
# Every chart and stat in the project's files, or any node that draws a card, drawn from its data, and the files
|
|
55
|
+
# that couldn't be read.
|
|
56
|
+
# The charts are found again when asked, and otherwise drawn again, so they follow changes to their data.
|
|
57
|
+
def charts(find: false)
|
|
58
|
+
if find || @charts.nil?
|
|
59
|
+
@file_errors = []
|
|
60
|
+
@charts = Projects.files(root).flat_map { |path| charts_in(path) }
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
{ cards: drawn, errors: @load_errors + @file_errors }
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
# Every endpoint in the project's files that writes its response back, so it can run, by its request, leaving out
|
|
67
|
+
# the ones whose snippet can't be read.
|
|
68
|
+
def endpoints
|
|
69
|
+
Projects.files(root).flat_map do |path|
|
|
70
|
+
document = document(path)
|
|
71
|
+
# Most files have no endpoint, so they aren't parsed for one.
|
|
72
|
+
next [] unless document.markdown.include?('endpoint')
|
|
73
|
+
|
|
74
|
+
runnable = document.directives.select { it.endpoint? && it.writes }
|
|
75
|
+
runnable.filter_map { |directive| endpoint(path, document.markdown.lines, directive) }
|
|
76
|
+
rescue StandardError
|
|
77
|
+
[]
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
# Markdown in one of the project's files, as Raindown would write it back, with its nodes, as { output, warnings }.
|
|
82
|
+
def rewrite_markdown(markdown, path)
|
|
83
|
+
Editing.rewrite_markdown(markdown, path:, bare: @bare)
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
# The names of the nodes that rasterize, Raindown's own and the project's, for a file in it.
|
|
87
|
+
def rasterizing(path)
|
|
88
|
+
Rasters.names(path)
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
# The project's own nodes, from its .raindown folder, as an app lists them, and why any couldn't be loaded.
|
|
92
|
+
def nodes
|
|
93
|
+
{ nodes: ProjectNodes.of(root), errors: @load_errors }
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
# What the node on a line of a file that rasterizes, like weather:, shows behind its text, and how it formats its
|
|
97
|
+
# lines, as { painting:, formatting: }, with its options, like width: and height:.
|
|
98
|
+
def repaint(path, line, **options)
|
|
99
|
+
Rasters.repaint(path:, line:, **options)
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def write_back(path)
|
|
103
|
+
Actions.write_back(path, bare: @bare, execute: @execute)
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
# Runs the endpoint on the line, replacing its response.
|
|
107
|
+
def execute(path, line)
|
|
108
|
+
directive = Document.read(path, bare: @bare).directives.find { it.line == line && it.endpoint? }
|
|
109
|
+
raise Actions::NotAnEndpoint, "There's no endpoint on line #{line}" unless directive
|
|
110
|
+
|
|
111
|
+
Actions.execute(path, directive)
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
private
|
|
115
|
+
|
|
116
|
+
def endpoint(path, lines, directive)
|
|
117
|
+
snippet = CodeBlocks.endpoint(lines, directive.line).first&.dig(:body)
|
|
118
|
+
variables = EndpointVariables.new(path:, content: lines.join)
|
|
119
|
+
request = Curl.request(snippet.to_s, variables)
|
|
120
|
+
Endpoint.new(project: root, path:, line: directive.line, method: request.method, url: variables.redact(request.url)) if request
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
def charts_in(path)
|
|
124
|
+
document = document(path)
|
|
125
|
+
lines = document.markdown.lines
|
|
126
|
+
# Its headings are only parsed when it has a chart, as most files don't.
|
|
127
|
+
NodeBlocks.find(lines, front_matter: FrontMatter.parse(document.markdown).first, bare: document.bare)
|
|
128
|
+
.select { it[:node].respond_to?(:card_lines) }
|
|
129
|
+
.map { |block| chart(block, path:, lines:, headings: document.headings) }
|
|
130
|
+
rescue StandardError => e
|
|
131
|
+
@file_errors << "#{path.delete_prefix(File.join(root, ''))}: #{e.message}"
|
|
132
|
+
[]
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
# The file's Document, read and parsed again only once the file's changed, so its endpoints, charts and outline
|
|
136
|
+
# share it, and finding them again only parses the files that changed.
|
|
137
|
+
def document(path)
|
|
138
|
+
stat = File.stat(path)
|
|
139
|
+
changed = [stat.mtime, stat.size]
|
|
140
|
+
cached_at, document = (@documents ||= {})[path]
|
|
141
|
+
return document if cached_at == changed
|
|
142
|
+
|
|
143
|
+
Document.read(path, bare: @bare).tap { @documents[path] = [changed, it] }
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
def drawn
|
|
147
|
+
pages = {}
|
|
148
|
+
@charts.map do |chart|
|
|
149
|
+
lines = lines(chart, pages[File.dirname(chart[:path])] ||= Pages.for(chart[:path]))
|
|
150
|
+
Card.new(project: root, chart: lines, **chart.except(:node))
|
|
151
|
+
end
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
def chart(block, path:, lines:, headings:)
|
|
155
|
+
line = block[:open] + 1
|
|
156
|
+
directive = Directives.text(lines[block[:open]])
|
|
157
|
+
|
|
158
|
+
{ path:, line:, heading: headings.select { it.line < line }.last&.text, directive:, node: block[:node] }
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
# Draws the chart or stat as its Markdown would be written back, with the files its project lists.
|
|
162
|
+
def lines(chart, metadata)
|
|
163
|
+
chart[:node].card_lines(metadata:)
|
|
164
|
+
rescue StandardError => e
|
|
165
|
+
["Can't chart: #{e.message}"]
|
|
166
|
+
end
|
|
167
|
+
end
|
|
168
|
+
end
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'nodes'
|
|
4
|
+
|
|
5
|
+
module Raindown
|
|
6
|
+
# A project's own nodes, from its .raindown folder, as plain data for an app to list: each one's name, whether it's
|
|
7
|
+
# Ruby, JavaScript or Markdown, its file, from the project's root, and whether it rasterizes. They're the nodes loaded
|
|
8
|
+
# in the process, so a project's Worker lists its own.
|
|
9
|
+
module ProjectNodes
|
|
10
|
+
module_function
|
|
11
|
+
|
|
12
|
+
def of(root)
|
|
13
|
+
folder = File.join(root, Nodes::FOLDER)
|
|
14
|
+
return [] unless File.directory?(folder)
|
|
15
|
+
|
|
16
|
+
Nodes.load(folder)
|
|
17
|
+
(ruby(folder) + javascript(folder) + markdown(folder)).map { listed(root, **it) }.sort_by { it[:name] }
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# By where they're defined, so nothing else is loaded to find them.
|
|
21
|
+
def ruby(folder)
|
|
22
|
+
Object.constants.filter_map do |name|
|
|
23
|
+
file, = Object.const_source_location(name)
|
|
24
|
+
next unless file&.start_with?("#{folder}/")
|
|
25
|
+
|
|
26
|
+
klass = Object.const_get(name)
|
|
27
|
+
{ name: name.to_s, kind: 'ruby', file:, node: klass } if Nodes.node?(klass)
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def javascript(folder)
|
|
32
|
+
JSNodes.node_classes.filter_map do |name, node|
|
|
33
|
+
{ name:, kind: 'javascript', file: node.file, node: } if node.file && File.dirname(node.file) == folder
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def markdown(folder)
|
|
38
|
+
MarkdownNodes.node_classes.filter_map do |name, node|
|
|
39
|
+
{ name:, kind: 'markdown', file: node.path, node: } if File.dirname(node.path) == folder
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def listed(root, name:, kind:, file:, node:)
|
|
44
|
+
{ name:, kind:, file: file.delete_prefix("#{root}/"), rasterizes: node.method_defined?(:rasterize) }
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
data/lib/raindown/projects.rb
CHANGED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'bare_directives'
|
|
4
|
+
require_relative 'front_matter'
|
|
5
|
+
require_relative 'grid_directives'
|
|
6
|
+
require_relative 'node_blocks'
|
|
7
|
+
require_relative 'nodes'
|
|
8
|
+
require_relative 'outdent'
|
|
9
|
+
require_relative 'pages/pages'
|
|
10
|
+
require_relative 'area'
|
|
11
|
+
require_relative 'scene'
|
|
12
|
+
require_relative 'utils'
|
|
13
|
+
|
|
14
|
+
module Raindown
|
|
15
|
+
# The nodes that rasterize, painting pixels behind their text in an app that draws pixels, like weather:'s sky. An
|
|
16
|
+
# app finds their blocks by name, then asks for each one's pixels by its directive's line.
|
|
17
|
+
module Rasters
|
|
18
|
+
module_function
|
|
19
|
+
|
|
20
|
+
# The names of Raindown's own nodes that rasterize, like weather, and, for a file, its project's Ruby and JavaScript
|
|
21
|
+
# Components that do.
|
|
22
|
+
def names(path = nil)
|
|
23
|
+
built_in = Nodes::BUILT_INS.select { |_, name| Raindown.const_get(name).method_defined?(:rasterize) }.keys
|
|
24
|
+
return built_in unless path
|
|
25
|
+
|
|
26
|
+
Nodes.load(path)
|
|
27
|
+
folder = Nodes.find_folder(File.dirname(File.expand_path(path)))
|
|
28
|
+
return built_in unless folder
|
|
29
|
+
|
|
30
|
+
built_in + ruby_rasterizing(folder) + JSNodes.rasterizing(folder)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
# The names of the Ruby nodes in a project's folder that rasterize, by where they're defined, so nothing else is
|
|
34
|
+
# loaded to find them.
|
|
35
|
+
def ruby_rasterizing(folder)
|
|
36
|
+
Object.constants.filter_map do |name|
|
|
37
|
+
file, = Object.const_source_location(name)
|
|
38
|
+
next unless file&.start_with?("#{folder}/")
|
|
39
|
+
|
|
40
|
+
klass = Object.const_get(name)
|
|
41
|
+
name.to_s if Nodes.node?(klass) && klass.method_defined?(:rasterize)
|
|
42
|
+
end.sort
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
# What the node whose directive's on the line, counted from 0, in the file shows behind its text, for an app, and
|
|
46
|
+
# how it formats the characters of its lines, as { painting:, formatting: }, in an Area of the width and height,
|
|
47
|
+
# in pixels, with where the text ends on each of its rows, and a character's width and height, as cell. A node in a
|
|
48
|
+
# grid's cell is by the column its cell starts at, after its |.
|
|
49
|
+
def repaint(path:, line:, column: nil, **space)
|
|
50
|
+
Nodes.load(path)
|
|
51
|
+
node = node_on(path, line, column)
|
|
52
|
+
raise ArgumentError, "Nothing on line #{line + 1} of #{path} rasterizes" unless node
|
|
53
|
+
|
|
54
|
+
metadata = Pages.for(path)
|
|
55
|
+
area = Area.new(**space)
|
|
56
|
+
{ painting: painting_of(node, area, metadata), formatting: formatting(node, metadata) }
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
# What the node shows: a Scene, for one of an app's own painters to paint, like weather:'s valley, or else the area
|
|
60
|
+
# it painted, painted again in the milliseconds it gave, if it did.
|
|
61
|
+
def painting_of(node, area, metadata)
|
|
62
|
+
painted = Nodes.call(node, :rasterize, area:, metadata:)
|
|
63
|
+
return painted.to_h if painted.is_a?(Scene)
|
|
64
|
+
|
|
65
|
+
again = painted if painted.is_a?(Numeric)
|
|
66
|
+
area.to_h(again)
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
# The characters of a node's lines to dim, to embolden and to draw as large as a line is tall, as { quiet:, bold:,
|
|
70
|
+
# large: }, each [line, from, to], its lines counted from the first it writes, from its formatting, or none.
|
|
71
|
+
def formatting(node, metadata)
|
|
72
|
+
return {} unless node.respond_to?(:formatting)
|
|
73
|
+
|
|
74
|
+
node.formatting(metadata:)
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
# The node on the line, read with outdent blocks' lines without their <, as anywhere else, or in the grid cell at
|
|
78
|
+
# the column.
|
|
79
|
+
def node_on(path, line, column)
|
|
80
|
+
content = Utils.read_markdown(path)
|
|
81
|
+
lines = Outdent.without_markers(content).lines
|
|
82
|
+
return cell_node(lines, line, column) if column
|
|
83
|
+
|
|
84
|
+
bare = BareDirectives.enabled?(path:, content:)
|
|
85
|
+
blocks = NodeBlocks.find(lines, front_matter: FrontMatter.parse(content).first, bare:)
|
|
86
|
+
blocks.find { it[:open] == line && it[:node].respond_to?(:rasterize) }&.dig(:node)
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
# The node whose directive starts the grid cell at the column of the line, up to the cell's next |.
|
|
90
|
+
def cell_node(lines, line, column)
|
|
91
|
+
text = lines[line].to_s[column..].to_s.split('|').first.to_s.strip
|
|
92
|
+
return unless GridDirectives.directive?(text)
|
|
93
|
+
|
|
94
|
+
blocks = NodeBlocks.find(["#{text}\n"], bare: true)
|
|
95
|
+
blocks.find { it[:node].respond_to?(:rasterize) }&.dig(:node)
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
end
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Raindown
|
|
4
|
+
# What a node shows behind its text in an app that draws pixels, from its rasterize, for the app to paint itself,
|
|
5
|
+
# like weather:'s valley: the name of a painter the app has, like valley, and the data it paints, like branches.
|
|
6
|
+
# A node that paints its own pixels paints its area instead.
|
|
7
|
+
Scene = Data.define(:name, :data) do
|
|
8
|
+
def to_h = { painter: name, data: }
|
|
9
|
+
end
|
|
10
|
+
end
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'net/http'
|
|
4
|
+
require 'openssl'
|
|
5
|
+
|
|
6
|
+
module Raindown
|
|
7
|
+
class Services
|
|
8
|
+
# Connections to the web, kept open after a request for the next to the same host, so a component asking a host for
|
|
9
|
+
# many things, like a map's tiles, doesn't open a connection, with its TLS handshake, for each. Each is used by one
|
|
10
|
+
# request at a time, and Net::HTTP opens it again when it's been idle too long, or retries a request once when the
|
|
11
|
+
# host had closed it.
|
|
12
|
+
module Connections
|
|
13
|
+
# How long a connection's kept idle, in seconds, and how many are kept for each host.
|
|
14
|
+
KEEP_FOR = 30
|
|
15
|
+
MOST_KEPT = 8
|
|
16
|
+
|
|
17
|
+
@idle = Hash.new { |hash, key| hash[key] = [] }
|
|
18
|
+
@lock = Mutex.new
|
|
19
|
+
|
|
20
|
+
module_function
|
|
21
|
+
|
|
22
|
+
# The response to a request, sent on a kept connection to its URI's host, or a new one.
|
|
23
|
+
def request(uri, http_request, timeout:)
|
|
24
|
+
key = [uri.scheme, uri.host, uri.port]
|
|
25
|
+
connection = take(key) || opened(uri, timeout)
|
|
26
|
+
response = connection.request(http_request)
|
|
27
|
+
keep(key, connection)
|
|
28
|
+
response
|
|
29
|
+
rescue StandardError
|
|
30
|
+
close(connection)
|
|
31
|
+
raise
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def opened(uri, timeout)
|
|
35
|
+
connection = Net::HTTP.new(uri.host, uri.port)
|
|
36
|
+
connection.use_ssl = uri.scheme == 'https'
|
|
37
|
+
connection.open_timeout = timeout
|
|
38
|
+
connection.read_timeout = timeout
|
|
39
|
+
connection.keep_alive_timeout = KEEP_FOR
|
|
40
|
+
connection.start
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def take(key)
|
|
44
|
+
@lock.synchronize { @idle[key].pop }
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def keep(key, connection)
|
|
48
|
+
kept = @lock.synchronize { @idle[key].size < MOST_KEPT && @idle[key].push(connection) }
|
|
49
|
+
close(connection) unless kept
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def close(connection)
|
|
53
|
+
connection.finish if connection&.started?
|
|
54
|
+
rescue IOError
|
|
55
|
+
nil
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
# Forgets the kept connections, closing them.
|
|
59
|
+
def clear
|
|
60
|
+
idle = @lock.synchronize { @idle.values.flatten.tap { @idle.clear } }
|
|
61
|
+
idle.each { close(it) }
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
end
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'digest'
|
|
4
|
+
require 'fileutils'
|
|
5
|
+
require 'json'
|
|
6
|
+
require 'net/http'
|
|
7
|
+
require 'open3'
|
|
8
|
+
require 'openssl'
|
|
9
|
+
require_relative 'connections'
|
|
10
|
+
|
|
11
|
+
module Raindown
|
|
12
|
+
class Services
|
|
13
|
+
# The services on the desktop and in the terminal, in Raindown's own process: the web with Net::HTTP, keeping its
|
|
14
|
+
# connections open for the next request to a host, the project's files, a cache in ~/.cache/raindown and git,
|
|
15
|
+
# which it runs.
|
|
16
|
+
class Local
|
|
17
|
+
TIMEOUT = 10
|
|
18
|
+
METHODS = %w[Get Head Post Put Patch Delete].freeze
|
|
19
|
+
REDIRECTS = 3
|
|
20
|
+
|
|
21
|
+
# A call's answer, from the project's services, by its root.
|
|
22
|
+
def self.call(root, name, args)
|
|
23
|
+
@at ||= {}
|
|
24
|
+
(@at[root] ||= new(root:)).public_send(name, *args)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
attr_reader :root
|
|
28
|
+
|
|
29
|
+
def initialize(root:)
|
|
30
|
+
@root = File.expand_path(root)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
# The response to a request, as { status, headers, body }, following redirects, its body as text.
|
|
34
|
+
def fetch(url, options = {})
|
|
35
|
+
options = options.transform_keys(&:to_s)
|
|
36
|
+
response = request(URI(url), options)
|
|
37
|
+
return redirected(url, response, options) if redirect?(response, options)
|
|
38
|
+
|
|
39
|
+
headers = response.to_hash.transform_values { it.join(', ') }
|
|
40
|
+
{ 'status' => response.code.to_i, 'headers' => headers, 'body' => body_of(response, options) }
|
|
41
|
+
rescue SocketError, SystemCallError, IOError, Timeout::Error, OpenSSL::SSL::SSLError => e
|
|
42
|
+
raise Error, "Couldn't reach #{url}: #{e.message}"
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
# A file's text, or its bytes as Base64 with encoding: 'base64', for a file that isn't text, like an image.
|
|
46
|
+
def read(path, options = {})
|
|
47
|
+
return [File.binread(inside(path))].pack('m0') if options.transform_keys(&:to_s)['encoding'] == 'base64'
|
|
48
|
+
|
|
49
|
+
File.read(inside(path))
|
|
50
|
+
rescue Errno::ENOENT
|
|
51
|
+
raise Error, "There's no #{path}"
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
# The project's files matching a pattern, like 'notes/*.md', from its root.
|
|
55
|
+
def list(pattern = '**/*')
|
|
56
|
+
Dir.glob(pattern, base: root).select { File.file?(File.join(root, it)) }.sort
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def write(path, text)
|
|
60
|
+
path = inside(path)
|
|
61
|
+
FileUtils.mkdir_p(File.dirname(path))
|
|
62
|
+
File.write(path, text)
|
|
63
|
+
nil
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
# What was kept under a key for the project, or nil when nothing was, or it's older than the seconds it was kept for.
|
|
67
|
+
def cache_get(key)
|
|
68
|
+
kept = JSON.parse(File.read(cache_file(key)))
|
|
69
|
+
return if kept['until'] && Time.now.to_f > kept['until']
|
|
70
|
+
|
|
71
|
+
kept['value']
|
|
72
|
+
rescue Errno::ENOENT, JSON::ParserError
|
|
73
|
+
nil
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
# Keeps a value JSON can hold under a key, for so many seconds, or until it's kept again.
|
|
77
|
+
def cache_set(key, value, seconds = nil)
|
|
78
|
+
FileUtils.mkdir_p(File.dirname(cache_file(key)))
|
|
79
|
+
File.write(cache_file(key), JSON.generate('until' => seconds && (Time.now.to_f + seconds), 'value' => value))
|
|
80
|
+
nil
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
# What git writes, run in the project with the arguments, like ('branch', '--list').
|
|
84
|
+
def git(*args)
|
|
85
|
+
output, errors, status = Open3.capture3('git', *args.map(&:to_s), chdir: root)
|
|
86
|
+
raise Error, git_error(args, errors) unless status.success?
|
|
87
|
+
|
|
88
|
+
output
|
|
89
|
+
rescue Errno::ENOENT
|
|
90
|
+
raise Error, 'git is not installed'
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
private
|
|
94
|
+
|
|
95
|
+
# Its body as UTF-8 text, or as Base64 with encoding: 'base64', for a file that isn't text, like an image.
|
|
96
|
+
def body_of(response, options)
|
|
97
|
+
return [response.body.to_s].pack('m0') if options['encoding'] == 'base64'
|
|
98
|
+
|
|
99
|
+
response.body.to_s.dup.force_encoding(Encoding::UTF_8).scrub
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def git_error(args, errors)
|
|
103
|
+
return "git #{args.first} failed" if errors.strip.empty?
|
|
104
|
+
|
|
105
|
+
errors.strip
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
def request(uri, options)
|
|
109
|
+
method = (options['method'] || 'GET').to_s.capitalize
|
|
110
|
+
raise Error, "Can't #{method.upcase} with fetch" unless METHODS.include?(method)
|
|
111
|
+
|
|
112
|
+
http_request = Net::HTTP.const_get(method).new(uri, options['headers'] || {})
|
|
113
|
+
http_request.body = options['body'] if options['body']
|
|
114
|
+
Connections.request(uri, http_request, timeout: TIMEOUT)
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
def redirected(url, response, options)
|
|
118
|
+
fetch(URI.join(url, response['location']).to_s, options.merge('redirects' => redirects_left(options) - 1))
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
def redirect?(response, options) = response.is_a?(Net::HTTPRedirection) && redirects_left(options).positive?
|
|
122
|
+
|
|
123
|
+
def redirects_left(options) = options.fetch('redirects', REDIRECTS)
|
|
124
|
+
|
|
125
|
+
# The path in the project, which it must be.
|
|
126
|
+
def inside(path)
|
|
127
|
+
full = File.expand_path(path.to_s, root)
|
|
128
|
+
raise Error, "#{path} is outside the project" unless full.start_with?("#{root}/")
|
|
129
|
+
|
|
130
|
+
full
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
def cache_file(key)
|
|
134
|
+
name = Digest::SHA256.hexdigest("#{root}\n#{key}")[0, 24]
|
|
135
|
+
File.join(ENV.fetch('XDG_CACHE_HOME', File.join(Dir.home, '.cache')), 'raindown', 'nodes', "#{name}.json")
|
|
136
|
+
end
|
|
137
|
+
end
|
|
138
|
+
end
|
|
139
|
+
end
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Raindown
|
|
4
|
+
# What a node can ask Raindown for, the same from Ruby and JavaScript, wherever it runs: the web, its project's files,
|
|
5
|
+
# a cache and git. A Ruby node gets them as raindown:, like metadata:, and a JavaScript one as raindown in its context,
|
|
6
|
+
# each asking through its host, which ends here, like an app's page asking its server. Each call is a name and
|
|
7
|
+
# arguments that JSON can hold, answered by Services.host: Local on the desktop and in the terminal, or an app's own,
|
|
8
|
+
# like the Obsidian plugin's worker, which can't run processes or open sockets. A platform without a service, like
|
|
9
|
+
# git on a phone, raises an Error, which a node can catch. Paths are its project's, from its root, and can't leave it.
|
|
10
|
+
class Services
|
|
11
|
+
# What can be asked for, by the names the hosts send.
|
|
12
|
+
NAMES = %w[fetch read list write cache_get cache_set git].freeze
|
|
13
|
+
|
|
14
|
+
class Error < StandardError; end
|
|
15
|
+
|
|
16
|
+
class << self
|
|
17
|
+
# What answers each call, with call(root, name, args), which an app can give its own.
|
|
18
|
+
attr_writer :host
|
|
19
|
+
|
|
20
|
+
def host
|
|
21
|
+
@host ||= begin
|
|
22
|
+
require_relative 'services/local'
|
|
23
|
+
Local
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# The services for a project, by its root, made once.
|
|
28
|
+
def at(root)
|
|
29
|
+
@at ||= {}
|
|
30
|
+
@at[File.expand_path(root)] ||= new(root:)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
# The services for the page being written, from its metadata's root, or else the current folder.
|
|
34
|
+
def for(metadata)
|
|
35
|
+
root = metadata.root if metadata.respond_to?(:root)
|
|
36
|
+
at(root || Dir.pwd)
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
attr_reader :root
|
|
41
|
+
|
|
42
|
+
def initialize(root:)
|
|
43
|
+
@root = File.expand_path(root)
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# A call by its name and arguments, as a host sends it.
|
|
47
|
+
def call(name, args = [])
|
|
48
|
+
raise Error, "There's no #{name} service" unless NAMES.include?(name.to_s)
|
|
49
|
+
|
|
50
|
+
self.class.host.call(root, name.to_s, args)
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
# The response to a request, as { status, headers, body }, following redirects, its body as text.
|
|
54
|
+
def fetch(url, options = {}) = call('fetch', [url, options])
|
|
55
|
+
|
|
56
|
+
def read(path, options = {}) = call('read', [path, options])
|
|
57
|
+
|
|
58
|
+
# The project's files matching a pattern, like 'notes/*.md', from its root.
|
|
59
|
+
def list(pattern = '**/*') = call('list', [pattern])
|
|
60
|
+
|
|
61
|
+
def write(path, text) = call('write', [path, text])
|
|
62
|
+
|
|
63
|
+
# What was kept under a key for the project, or nil when nothing was, or it's older than the seconds it was kept for.
|
|
64
|
+
def cache_get(key) = call('cache_get', [key])
|
|
65
|
+
|
|
66
|
+
# Keeps a value JSON can hold under a key, for so many seconds, or until it's kept again.
|
|
67
|
+
def cache_set(key, value, seconds = nil) = call('cache_set', [key, value, seconds])
|
|
68
|
+
|
|
69
|
+
# What git writes, run in the project with the arguments, like ('branch', '--list').
|
|
70
|
+
def git(*args) = call('git', args)
|
|
71
|
+
end
|
|
72
|
+
end
|
data/lib/raindown/skeleton.rb
CHANGED
data/lib/raindown/table/table.rb
CHANGED
|
@@ -72,9 +72,9 @@ module Raindown
|
|
|
72
72
|
TableFile.supported?(path) && Utils.inside?(path, data_root(metadata)) && !File.exist?(path)
|
|
73
73
|
end
|
|
74
74
|
|
|
75
|
-
# From the table as it's written
|
|
75
|
+
# From the table as it's written.
|
|
76
76
|
def create_file(path, lines)
|
|
77
|
-
rows = MarkdownTable.parse(lines
|
|
77
|
+
rows = MarkdownTable.parse(lines)
|
|
78
78
|
rows = STARTER if rows.empty?
|
|
79
79
|
TableFile.create(path, rows)
|
|
80
80
|
end
|
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require 'unicode/display_width'
|
|
4
|
-
|
|
5
3
|
module Raindown
|
|
6
4
|
# A table's column: its alignment, from its delimiter cell's colons, and its width, that of its widest cell, or as wide
|
|
7
5
|
# as its delimiter when that's equals signs, like |====|, which is fixed. An equals sign typed into dashes, like
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
# Loaded when first used, as it loads strscan, a native extension, which slows a packed executable's start.
|
|
4
|
+
autoload :CSV, 'csv'
|
|
5
5
|
|
|
6
6
|
module Raindown
|
|
7
7
|
# The CSV or TSV file behind a table:, as rows of cells as wide as each other, written back in its own format.
|