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,43 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Raindown
|
|
4
|
+
# A grid: row's cells that start with a node's directive, other than a grid, one of Raindown's own, like chart:, or
|
|
5
|
+
# the project's, like a JavaScript Component, which run as anywhere else, writing their output into the cell's lines
|
|
6
|
+
# below them, taking the cell's blank lines at its end, so the row stays as tall as it was if they're enough.
|
|
7
|
+
module GridDirectives
|
|
8
|
+
DIRECTIVE = /\A([A-Za-z][\w-]*):(?=\s|\z)/
|
|
9
|
+
|
|
10
|
+
module_function
|
|
11
|
+
|
|
12
|
+
# The row with each of its cells' directives run, as tall as it was unless one's output needs more lines than its
|
|
13
|
+
# cell has, for the file's metadata, so a project's node has its services.
|
|
14
|
+
def run(row, metadata: NoMetadata)
|
|
15
|
+
cells = Array.new(row.map(&:size).max) { |column| row.map { it[column].to_s } }
|
|
16
|
+
cells = cells.map { written(it, metadata) }
|
|
17
|
+
Array.new(cells.map(&:size).max) { |line| cells.map { it[line].to_s } }
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# The columns of the row's cells that start with a directive, whose lines are kept as they're wrapped.
|
|
21
|
+
def columns(row)
|
|
22
|
+
row.first.to_a.each_index.select { |column| directive?(row.first[column].to_s) }
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def written(lines, metadata)
|
|
26
|
+
return lines unless directive?(lines.first.to_s)
|
|
27
|
+
|
|
28
|
+
require_relative 'node_injector'
|
|
29
|
+
written = NodeInjector.rewrite(content: "#{lines.join("\n")}\n", bare: true, metadata:).lines.map(&:chomp)
|
|
30
|
+
written.pop while written.size > lines.size && written.last == ''
|
|
31
|
+
written + Array.new([lines.size - written.size, 0].max, '')
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# Whether a cell's first line is a node's directive, but a grid's, and not one that only looks like one, like a
|
|
35
|
+
# note's "Note: hi".
|
|
36
|
+
def directive?(line)
|
|
37
|
+
name = line[DIRECTIVE, 1]
|
|
38
|
+
return false if name.nil? || name == 'grid'
|
|
39
|
+
|
|
40
|
+
!Nodes.node_class(name).nil?
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Raindown
|
|
4
|
+
# A grid: row's cells' text wrapped to their widths, unless they fit it.
|
|
5
|
+
module GridWrapping
|
|
6
|
+
module_function
|
|
7
|
+
|
|
8
|
+
# The row with each cell's text wrapped at its spaces to its width: each paragraph, its lines up to a blank one,
|
|
9
|
+
# written again from its words, the row taller when they need more lines, never shorter. A word longer than its cell,
|
|
10
|
+
# like an image's path, runs on past it. The cells in kept, by their columns, keep their lines, like a directive's
|
|
11
|
+
# and its output's, each wrapped on its own, taking blank lines at the cell's end for the lines that adds.
|
|
12
|
+
def wrapped(row, widths, kept: [])
|
|
13
|
+
columns = widths.each_with_index.map do |width, column|
|
|
14
|
+
texts = row.map { it[column].to_s }
|
|
15
|
+
kept.include?(column) ? kept_lines(texts, width - 2) : wrapped_lines(texts, width - 2)
|
|
16
|
+
end
|
|
17
|
+
Array.new([row.size, *columns.map(&:size)].max) { |line| columns.map { it[line] } }
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def kept_lines(texts, width)
|
|
21
|
+
lines = texts.flat_map { it.empty? ? [it] : lines_of(it, width) }
|
|
22
|
+
lines.pop while lines.size > texts.size && lines.last.empty?
|
|
23
|
+
lines
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def wrapped_lines(texts, width)
|
|
27
|
+
texts.chunk_while { |line, following| !line.empty? && !following.empty? }.flat_map do |lines|
|
|
28
|
+
next lines if lines.first.empty?
|
|
29
|
+
|
|
30
|
+
lines_of(lines.join(' '), width)
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# The text's lines, broken at the spaces after its words, each word keeping the spaces after it unless it ends a
|
|
35
|
+
# line, so spaces typed between words, like one before another as a word's typed between two, are kept.
|
|
36
|
+
def lines_of(text, width)
|
|
37
|
+
text.scan(/\S+\s*/).each_with_object([+'']) do |word, lines|
|
|
38
|
+
if lines.last.empty? || Unicode::DisplayWidth.of(lines.last + word.rstrip, emoji: :all) <= width
|
|
39
|
+
lines.last << word
|
|
40
|
+
else
|
|
41
|
+
lines << +word
|
|
42
|
+
end
|
|
43
|
+
end.map(&:rstrip)
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'json'
|
|
4
|
+
|
|
5
|
+
module Raindown
|
|
6
|
+
# JSON from the web, for nodes that ask an API, like painting:'s museum and weather:'s forecast, following a redirect
|
|
7
|
+
# or two, and raising an Error when it can't be reached or doesn't answer with JSON. Net::HTTP is loaded when it's
|
|
8
|
+
# first asked, as where there are no sockets, like WebAssembly in the Obsidian plugin, it can't be.
|
|
9
|
+
module HTTPJSON
|
|
10
|
+
TIMEOUT = 10
|
|
11
|
+
|
|
12
|
+
class Error < StandardError; end
|
|
13
|
+
|
|
14
|
+
module_function
|
|
15
|
+
|
|
16
|
+
def get(url, headers: {}, redirects: 3)
|
|
17
|
+
load_web
|
|
18
|
+
json_from(url, headers, redirects)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def load_web
|
|
22
|
+
require 'net/http'
|
|
23
|
+
require 'openssl'
|
|
24
|
+
rescue LoadError
|
|
25
|
+
raise Error, "Raindown can't reach the web here"
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def json_from(url, headers, redirects)
|
|
29
|
+
response = request(URI(url), headers)
|
|
30
|
+
redirect = response.is_a?(Net::HTTPRedirection) && redirects.positive?
|
|
31
|
+
return get(URI.join(url, response['location']).to_s, headers:, redirects: redirects - 1) if redirect
|
|
32
|
+
raise Error, "#{url} answered #{response.code}" unless response.is_a?(Net::HTTPSuccess)
|
|
33
|
+
|
|
34
|
+
JSON.parse(response.body)
|
|
35
|
+
rescue SocketError, SystemCallError, Timeout::Error, JSON::ParserError, OpenSSL::SSL::SSLError => e
|
|
36
|
+
raise Error, "Couldn't reach #{url}: #{e.message}"
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def request(uri, headers)
|
|
40
|
+
Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https', open_timeout: TIMEOUT, read_timeout: TIMEOUT) do |http|
|
|
41
|
+
http.request(Net::HTTP::Get.new(uri, headers))
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'cgi/escape'
|
|
4
|
+
require_relative 'node'
|
|
5
|
+
require_relative 'problem'
|
|
6
|
+
|
|
7
|
+
module Raindown
|
|
8
|
+
# The node behind image:, which writes a Markdown image into the file, so it shows wherever Markdown does:
|
|
9
|
+
# image: 'pictures/cat.png' image: 'pictures/cat.png' alt: 'A cat' width: 40
|
|
10
|
+
# The source is a path relative to the note, as Markdown's are, or a URL. Alt describes the image, and is its file's
|
|
11
|
+
# name by default. Width is how many characters wide to show it, in the HTML and in editors that draw images in text,
|
|
12
|
+
# which read it from the directive.
|
|
13
|
+
class Image < Node
|
|
14
|
+
def initialize(source = nil, alt: nil, width: nil)
|
|
15
|
+
@source = source
|
|
16
|
+
@alt = alt
|
|
17
|
+
@width = width
|
|
18
|
+
super()
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def rewrite
|
|
22
|
+
return Problem.alert('Needs a path to an image or a URL') if ['', nil].include?(@source)
|
|
23
|
+
|
|
24
|
+
""
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def render
|
|
28
|
+
return super unless @width
|
|
29
|
+
|
|
30
|
+
%(<p><img src="#{CGI.escapeHTML(@source.to_s)}" alt="#{CGI.escapeHTML(alt)}" style="width: #{@width.to_i}ch"></p>)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
private
|
|
34
|
+
|
|
35
|
+
def alt
|
|
36
|
+
(@alt || File.basename(@source.to_s, '.*')).delete('[]')
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# A path with spaces goes in angle brackets, which Markdown allows.
|
|
40
|
+
def destination
|
|
41
|
+
@source.match?(/\s/) ? "<#{@source}>" : @source
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
data/lib/raindown/js/js_nodes.rb
CHANGED
|
@@ -1,55 +1,84 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require 'json'
|
|
4
|
-
require 'open3'
|
|
5
4
|
|
|
6
5
|
require_relative '../node'
|
|
6
|
+
require_relative '../scene'
|
|
7
|
+
require_relative '../services'
|
|
7
8
|
|
|
8
9
|
module Raindown
|
|
9
10
|
# JavaScript Components, the .js and .mjs files in a project's .raindown folder, which export classes with rewrite and
|
|
10
|
-
# optionally render, like Ruby nodes. They run in
|
|
11
|
-
#
|
|
11
|
+
# optionally render, like Ruby nodes. They run in QuickJS, inside Raindown, so they need nothing installed, or in a
|
|
12
|
+
# host of an app's own, like its page. Wherever they run, they get Raindown's services as raindown in their context.
|
|
13
|
+
# A host answers requests, which JSON can hold, with request(**message):
|
|
14
|
+
# { op: 'load', folder } → { nodes: [{ name, render, rasterize, file }], errors: [{ file, error }] }
|
|
15
|
+
# { op: 'call', calls: [{ id, name, args, props, method, context, root }] }
|
|
16
|
+
# → { answers: [{ value } or { error }] }
|
|
17
|
+
# and raises an Error for a request that fails. A call makes its node, with its args and props, the first time its id
|
|
18
|
+
# is called, so a node takes one request, and calls go together, so a note's nodes take one between them.
|
|
12
19
|
module JSNodes
|
|
13
20
|
EXTENSIONS = %w[.js .mjs].freeze
|
|
14
|
-
|
|
21
|
+
# Raindown's services for JavaScript nodes, which every host runs before its own script, like an app's page.
|
|
22
|
+
SERVICES = File.join(__dir__, 'services.js')
|
|
15
23
|
|
|
16
24
|
class Error < StandardError; end
|
|
17
25
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
def initialize
|
|
22
|
-
@input, @output, = Open3.popen2('node', '--input-type=module', '-e', File.read(SCRIPT))
|
|
23
|
-
rescue Errno::ENOENT
|
|
24
|
-
raise Error, 'JavaScript Components need NodeJS 22 or later on your PATH'
|
|
25
|
-
end
|
|
26
|
-
|
|
27
|
-
def request(**message)
|
|
28
|
-
@input.puts(JSON.generate(message))
|
|
29
|
-
reply = JSON.parse(@output.gets || raise(Error, 'NodeJS stopped running the JavaScript Components'))
|
|
30
|
-
raise Error, reply['error'] if reply['error']
|
|
31
|
-
|
|
32
|
-
reply
|
|
33
|
-
end
|
|
26
|
+
class << self
|
|
27
|
+
# A host of an app's own, answering the same requests, like the Obsidian plugin's worker or the desktop app's page.
|
|
28
|
+
attr_writer :host
|
|
34
29
|
end
|
|
35
30
|
|
|
36
31
|
module_function
|
|
37
32
|
|
|
38
|
-
# Loads the JavaScript Components in a .raindown folder, starting
|
|
33
|
+
# Loads the JavaScript Components in a .raindown folder, starting their host the first time a folder has any, and
|
|
34
|
+
# gives why any file couldn't be loaded, like ".raindown/sky.mjs: SyntaxError: …", or why none could, when the host
|
|
35
|
+
# couldn't load them, so the folder's other nodes still load.
|
|
39
36
|
def load(folder)
|
|
40
|
-
return if Dir.glob(EXTENSIONS.map { "*#{it}" }, base: folder).empty?
|
|
37
|
+
return [] if Dir.glob(EXTENSIONS.map { "*#{it}" }, base: folder).empty?
|
|
41
38
|
|
|
42
|
-
host.request(op: 'load', folder:)
|
|
43
|
-
|
|
44
|
-
|
|
39
|
+
reply = host.request(op: 'load', folder:)
|
|
40
|
+
reply.fetch('nodes').each { add(folder, it) }
|
|
41
|
+
reply.fetch('errors', []).map { "#{Nodes::FOLDER}/#{it['file']}: #{it['error']}" }
|
|
42
|
+
rescue Error => e
|
|
43
|
+
["#{Nodes::FOLDER}: #{e.message}"]
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# A node as the host described it, unless one of its name is already loaded.
|
|
47
|
+
def add(folder, node)
|
|
48
|
+
file = node['file'] && File.join(folder, node['file'])
|
|
49
|
+
node_classes[node['name']] ||= JSNode.for(name: node['name'], render: node['render'], rasterize: node['rasterize'], file:)
|
|
45
50
|
end
|
|
46
51
|
|
|
47
52
|
def node_class(name)
|
|
48
53
|
node_classes[name]
|
|
49
54
|
end
|
|
50
55
|
|
|
56
|
+
# The answers to calls of JavaScript nodes' methods, from JSNode#call_for, asked of their host together: each one's
|
|
57
|
+
# output, or an Error for one that raised.
|
|
58
|
+
def call(calls)
|
|
59
|
+
return [] if calls.empty?
|
|
60
|
+
|
|
61
|
+
host.request(op: 'call', calls:).fetch('answers').map do |answer|
|
|
62
|
+
next Error.new(answer['error']) if answer['error']
|
|
63
|
+
|
|
64
|
+
answer['value'].to_s
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
# The names of the folder's JavaScript Components that rasterize.
|
|
69
|
+
def rasterizing(folder)
|
|
70
|
+
node_classes.select { |_, node| node.method_defined?(:rasterize) && File.dirname(node.file) == folder }.keys
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
# QuickJS, unless an app gives its own. QuickJS is native, so it's only loaded here, not where an app gives its
|
|
74
|
+
# own host, like WebAssembly in the Obsidian plugin, and isn't installed on Windows, where it doesn't build.
|
|
51
75
|
def host
|
|
52
|
-
@host ||=
|
|
76
|
+
@host ||= begin
|
|
77
|
+
require_relative 'quickjs_host'
|
|
78
|
+
QuickJSHost.new
|
|
79
|
+
end
|
|
80
|
+
rescue LoadError
|
|
81
|
+
raise Error, "JavaScript Components need the quickjs gem, which doesn't build on Windows"
|
|
53
82
|
end
|
|
54
83
|
|
|
55
84
|
def node_classes
|
|
@@ -57,35 +86,58 @@ module Raindown
|
|
|
57
86
|
end
|
|
58
87
|
end
|
|
59
88
|
|
|
60
|
-
# A JavaScript Component, made in
|
|
89
|
+
# A JavaScript Component, made in its host with its props, where its rewrite and render are sent.
|
|
61
90
|
class JSNode < Node
|
|
62
|
-
# A class for each JavaScript Component, which renders its own HTML when the component has render
|
|
63
|
-
|
|
91
|
+
# A class for each JavaScript Component, from its file, which renders its own HTML when the component has render,
|
|
92
|
+
# and rasterizes when it has rasterize.
|
|
93
|
+
def self.for(name:, render:, rasterize: false, file: nil)
|
|
64
94
|
Class.new(self) do
|
|
65
95
|
define_singleton_method(:name) { name }
|
|
96
|
+
define_singleton_method(:file) { file }
|
|
66
97
|
define_method(:render) { |**context| js_output(:render, context) } if render
|
|
98
|
+
define_method(:rasterize) { |metadata: NoMetadata, **| scene(metadata) } if rasterize && file
|
|
67
99
|
end
|
|
68
100
|
end
|
|
69
101
|
|
|
70
102
|
def initialize(*args, **props)
|
|
71
103
|
super()
|
|
72
|
-
|
|
104
|
+
@args = args
|
|
105
|
+
@props = props
|
|
73
106
|
end
|
|
74
107
|
|
|
75
108
|
def rewrite(**context)
|
|
76
109
|
js_output(:rewrite, context)
|
|
77
110
|
end
|
|
78
111
|
|
|
112
|
+
# A call of its method, for JSNodes.call, with what its host makes it with the first time, and its project's root,
|
|
113
|
+
# for its services, which its host gives it as raindown.
|
|
114
|
+
def call_for(method, context)
|
|
115
|
+
{ id: object_id, name: self.class.name, args: @args, props: @props, method:, root: Services.for(context[:metadata]).root,
|
|
116
|
+
context: json_context(**context) }
|
|
117
|
+
end
|
|
118
|
+
|
|
79
119
|
private
|
|
80
120
|
|
|
121
|
+
# What it shows behind its text, which an app paints by running its rasterize itself, every frame: the node scene,
|
|
122
|
+
# with its file's source, read again each time, so a change shows, its value and props, to make it with, and its
|
|
123
|
+
# project's root, for the services the app gives it as raindown.
|
|
124
|
+
def scene(metadata)
|
|
125
|
+
data = { node: self.class.name, source: File.read(self.class.file), args: @args, props: @props, root: Services.for(metadata).root }
|
|
126
|
+
Scene.new(name: 'node', data:)
|
|
127
|
+
end
|
|
128
|
+
|
|
81
129
|
def js_output(method, context)
|
|
82
|
-
JSNodes.
|
|
130
|
+
output = JSNodes.call([call_for(method, context)]).first
|
|
131
|
+
raise output if output.is_a?(JSNodes::Error)
|
|
132
|
+
|
|
133
|
+
output
|
|
83
134
|
end
|
|
84
135
|
|
|
85
|
-
# JSON can't hold Pages, so the metadata is its root and each file's front matter and path
|
|
136
|
+
# JSON can't hold Pages, so the metadata is its root and each file's front matter and path, nor the services, which
|
|
137
|
+
# its host makes again.
|
|
86
138
|
def json_context(metadata: NoMetadata, **context)
|
|
87
139
|
root = metadata.root if metadata.respond_to?(:root)
|
|
88
|
-
{ **context, metadata: { root:, files: metadata.list.map(&:metadata) } }
|
|
140
|
+
{ **context.except(:raindown), metadata: { root:, files: metadata.list.map(&:metadata) } }
|
|
89
141
|
end
|
|
90
142
|
end
|
|
91
143
|
end
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'json'
|
|
4
|
+
|
|
5
|
+
module Raindown
|
|
6
|
+
module JSNodes
|
|
7
|
+
# JavaScript Components run in QuickJS, inside Raindown's own process, so they need nothing installed, answering
|
|
8
|
+
# JSNodes' requests. Like a page, it has no Node APIs, but a node's file can import others by their paths
|
|
9
|
+
# from it, like '../lib/release.mjs', and has setTimeout, URL and TextEncoder, and Raindown's services. What it
|
|
10
|
+
# logs goes to stderr.
|
|
11
|
+
class QuickJSHost
|
|
12
|
+
RUNTIME = %w[services.js quickjs_runtime.js].map { File.join(__dir__, it) }
|
|
13
|
+
# How long a request can run, in milliseconds, its services' time included.
|
|
14
|
+
TIMEOUT = 60_000
|
|
15
|
+
|
|
16
|
+
def initialize
|
|
17
|
+
require 'quickjs'
|
|
18
|
+
@vm = Quickjs::VM.new(features: [Quickjs::FEATURE_TIMEOUT, Quickjs::POLYFILL_URL, Quickjs::POLYFILL_ENCODING],
|
|
19
|
+
timeout_msec: TIMEOUT)
|
|
20
|
+
@vm.module_loader = ->(specifier, importer) { module_of(specifier, importer) }
|
|
21
|
+
@vm.define_function('__raindownService') { |root, name, args| JSON.generate(Services.at(root).call(name, JSON.parse(args))) }
|
|
22
|
+
@vm.on_log { warn it }
|
|
23
|
+
@vm.eval_code(RUNTIME.map { File.read(it) }.join("\n"))
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def request(**message)
|
|
27
|
+
errors = import(message[:folder]) if message[:op] == 'load'
|
|
28
|
+
reply = JSON.parse(@vm.call('__raindown.request', JSON.generate(sent(message))))
|
|
29
|
+
raise Error, reply['error'] if reply['error']
|
|
30
|
+
|
|
31
|
+
reply.merge('errors' => errors || [])
|
|
32
|
+
rescue Quickjs::RuntimeError => e
|
|
33
|
+
raise Error, e.message
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
private
|
|
37
|
+
|
|
38
|
+
def files(folder) = Dir.glob(EXTENSIONS.map { "*#{it}" }, base: folder).sort.map { File.join(folder, it) }
|
|
39
|
+
|
|
40
|
+
def sent(message)
|
|
41
|
+
return message unless message[:op] == 'load'
|
|
42
|
+
|
|
43
|
+
{ op: 'load', files: files(message[:folder]) }
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# Each of the folder's files, but one that can't be, so the others still load, and why any couldn't be.
|
|
47
|
+
def import(folder)
|
|
48
|
+
files(folder).filter_map do |file|
|
|
49
|
+
@vm.import('* as exports', filename: file, code_to_expose: "__raindown.register(#{JSON.generate(file)}, exports)")
|
|
50
|
+
nil
|
|
51
|
+
rescue Quickjs::RuntimeError => e
|
|
52
|
+
{ 'file' => File.basename(file), 'error' => e.message }
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
# A module's source by its path from the file importing it, which must be a file.
|
|
57
|
+
def module_of(specifier, importer)
|
|
58
|
+
path = File.expand_path(specifier, File.dirname(importer.to_s))
|
|
59
|
+
{ code: File.read(path), as: path } if File.file?(path)
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
// QuickJS's side of a project's JavaScript Components, which QuickJSHost runs after services.js, answering the same
|
|
2
|
+
// requests as any host, as JSON, through __raindown.request. QuickJSHost imports each file of a folder, registering its
|
|
3
|
+
// exports, then asks to load them. A service is asked of Ruby through __raindownService, which it defines.
|
|
4
|
+
globalThis.__raindown = (() => {
|
|
5
|
+
const classes = {}
|
|
6
|
+
const fileOf = {}
|
|
7
|
+
const exportsOf = {}
|
|
8
|
+
const nodes = {}
|
|
9
|
+
const ask = async (root, name, args) => JSON.parse(__raindownService(root, name, JSON.stringify(args)))
|
|
10
|
+
|
|
11
|
+
const requests = {
|
|
12
|
+
// Every exported class with rewrite in the files is a node, named by its export, or by its class for a default one.
|
|
13
|
+
load({ files }) {
|
|
14
|
+
const loaded = []
|
|
15
|
+
for (const file of files) {
|
|
16
|
+
for (const [name, value] of Object.entries(exportsOf[file] ?? {})) {
|
|
17
|
+
if (typeof value?.prototype?.rewrite !== 'function') continue
|
|
18
|
+
const nodeName = name === 'default' ? value.name : name
|
|
19
|
+
classes[nodeName] = value
|
|
20
|
+
fileOf[nodeName] = file.split('/').at(-1)
|
|
21
|
+
loaded.push(nodeName)
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
const described = loaded.map((name) => ({
|
|
25
|
+
name,
|
|
26
|
+
render: typeof classes[name].prototype.render === 'function',
|
|
27
|
+
rasterize: typeof classes[name].prototype.rasterize === 'function',
|
|
28
|
+
file: fileOf[name]
|
|
29
|
+
}))
|
|
30
|
+
return { nodes: described }
|
|
31
|
+
},
|
|
32
|
+
|
|
33
|
+
// Each call makes its node the first time, and answers apart, so one that throws doesn't fail the others.
|
|
34
|
+
async call({ calls }) {
|
|
35
|
+
const answers = calls.map(async ({ id, name, args, props, method, context, root }) => {
|
|
36
|
+
try {
|
|
37
|
+
nodes[id] ??= new classes[name](props, ...args)
|
|
38
|
+
return { value: await nodes[id][method]({ ...context, raindown: raindownServices(root, ask) }) }
|
|
39
|
+
} catch (error) {
|
|
40
|
+
return { error: String(error?.message ?? error) }
|
|
41
|
+
}
|
|
42
|
+
})
|
|
43
|
+
return { answers: await Promise.all(answers) }
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return {
|
|
48
|
+
register(file, exports) {
|
|
49
|
+
exportsOf[file] = exports
|
|
50
|
+
},
|
|
51
|
+
|
|
52
|
+
async request(json) {
|
|
53
|
+
const message = JSON.parse(json)
|
|
54
|
+
try {
|
|
55
|
+
return JSON.stringify(await requests[message.op](message))
|
|
56
|
+
} catch (error) {
|
|
57
|
+
return JSON.stringify({ error: String(error?.message ?? error) })
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
})()
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
// Raindown's services for a JavaScript node, as raindown in its methods' context, the same wherever it runs: in
|
|
2
|
+
// QuickJS in the terminal, an app's page, the Obsidian plugin or a phone. Each asks the host with ask(root, name, args), which
|
|
3
|
+
// answers with a promise, from Raindown::Services on the desktop, so they're all async, and a service a platform hasn't
|
|
4
|
+
// got rejects. A plain script, not a module, so each host can put it before its own code.
|
|
5
|
+
function raindownServices(root, ask) {
|
|
6
|
+
const call = (name, ...args) => ask(root, name, args)
|
|
7
|
+
|
|
8
|
+
return {
|
|
9
|
+
root,
|
|
10
|
+
// { status, headers, body, json() }, its body as text, or as Base64 with encoding: 'base64', for a file that isn't text,
|
|
11
|
+
// like an image.
|
|
12
|
+
fetch: async (url, { method, headers, body, encoding } = {}) => {
|
|
13
|
+
const response = await call('fetch', url, { method, headers, body, encoding })
|
|
14
|
+
return { ...response, json: () => JSON.parse(response.body) }
|
|
15
|
+
},
|
|
16
|
+
// The project's files, by their paths from its root, which they can't leave, read as text, or as Base64 with
|
|
17
|
+
// encoding: 'base64', for a file that isn't text, like an image.
|
|
18
|
+
files: {
|
|
19
|
+
read: (path, { encoding } = {}) => call('read', path, { encoding }),
|
|
20
|
+
list: (pattern = '**/*') => call('list', pattern),
|
|
21
|
+
write: (path, text) => call('write', path, text)
|
|
22
|
+
},
|
|
23
|
+
// Values JSON can hold, kept for the project, for so many seconds, or until they're kept again.
|
|
24
|
+
cache: {
|
|
25
|
+
get: (key) => call('cache_get', key),
|
|
26
|
+
set: (key, value, { seconds } = {}) => call('cache_set', key, value, seconds ?? null)
|
|
27
|
+
},
|
|
28
|
+
// What git writes, run in the project, like git('branch', '--list'), where there's git.
|
|
29
|
+
git: (...args) => call('git', ...args)
|
|
30
|
+
}
|
|
31
|
+
}
|
|
@@ -19,11 +19,13 @@ module Raindown
|
|
|
19
19
|
|
|
20
20
|
module_function
|
|
21
21
|
|
|
22
|
+
# Its templates are read as they're written, so loading one can't fail, and there are never errors to give.
|
|
22
23
|
def load(folder)
|
|
23
24
|
Dir.glob(EXTENSIONS.map { "*#{it}" }, base: folder).each do |file|
|
|
24
25
|
name = File.basename(file, '.*')
|
|
25
26
|
node_classes[name] ||= MarkdownNode.for(name:, path: File.join(folder, file)) if name.match?(NAME)
|
|
26
27
|
end
|
|
28
|
+
[]
|
|
27
29
|
end
|
|
28
30
|
|
|
29
31
|
# Whether a file is a Markdown Component, by its extension and name, so .raindown/data/blueprints.md isn't one.
|
data/lib/raindown/node_blocks.rb
CHANGED
|
@@ -19,7 +19,8 @@ module Raindown
|
|
|
19
19
|
# A bare :Changelog is write-in: it only renders, with its render, never its rewrite. In Antlers' delimiters, like
|
|
20
20
|
# <{ Changelog }>, it's write-in too, and the template pass renders it, as a WriteInNode, so it's only found here
|
|
21
21
|
# for a document's outline. toc: and list: are Raindown's own nodes. A name only starts a block when it's a loaded
|
|
22
|
-
# node, so other text like "Note:" is left alone.
|
|
22
|
+
# node, so other text like "Note:" is left alone. Where bare directives work, a bare grid, a block starting with a
|
|
23
|
+
# rule like +---+, is a grid: without its directive, which it writes from its first line, its rule.
|
|
23
24
|
module NodeBlocks
|
|
24
25
|
class MissingEndTag < StandardError; end
|
|
25
26
|
class BrokenText < StandardError; end
|
|
@@ -31,6 +32,7 @@ module Raindown
|
|
|
31
32
|
# <{ Card }>, or <{ :Card }>, which a lowercase name like toc needs, as <{ toc }> is a variable.
|
|
32
33
|
SELF_CLOSING = /\A( {0,3})<\{\s*(?::|(?=[A-Z]))(#{NAME})(?:\s+(.*?))?\s*\}>[ \t]*$/
|
|
33
34
|
BARE_SELF_CLOSING = /\A( {0,3}):(#{NAME})(?:[ \t]+(.*?))?[ \t]*$/
|
|
35
|
+
BARE_GRID = /\A {0,3}\+-[-+]*\+[ \t]*$/
|
|
34
36
|
# Unquoted text after name:, like YAML's, runs up to the next prop, so title: Hello world limit: 3 has two. A comma
|
|
35
37
|
# before the next prop, like data.csv, total: true, separates them like a space, while one in text, like Hello,
|
|
36
38
|
# world, is part of it.
|
|
@@ -43,8 +45,9 @@ module Raindown
|
|
|
43
45
|
|
|
44
46
|
module_function
|
|
45
47
|
|
|
46
|
-
# Each block outside code and front matter as { open:, close:, last:, writes:, node: }, where
|
|
47
|
-
# bare block, and writes is whether its output's written into the file. A
|
|
48
|
+
# Each block outside code and front matter as { open:, first:, close:, last:, writes:, node: }, where first is the
|
|
49
|
+
# first line it wrote, close is nil for a bare block, and writes is whether its output's written into the file. A
|
|
50
|
+
# self-closing node is a block of one line.
|
|
48
51
|
def find(lines, front_matter: {}, bare: false)
|
|
49
52
|
with_nodes(spans(lines, bare:), front_matter)
|
|
50
53
|
end
|
|
@@ -113,15 +116,17 @@ module Raindown
|
|
|
113
116
|
end
|
|
114
117
|
|
|
115
118
|
def rendered_lines(lines, block, context)
|
|
116
|
-
|
|
119
|
+
written = Problem.without_alert(lines[block[:first]...(block[:close] || (block[:last] + 1))])
|
|
120
|
+
output = node_markdown(block, context, written:).chomp
|
|
117
121
|
close = marker(lines[block[:close]]) if block[:close]
|
|
118
122
|
|
|
119
|
-
[
|
|
123
|
+
[*lines[block[:open]...block[:first]].map { marker(it) }, *("#{output}\n".lines unless output.empty?), *close]
|
|
120
124
|
end
|
|
121
125
|
|
|
122
|
-
# A node that raises shows why in its place, rather than stopping the page.
|
|
123
|
-
|
|
124
|
-
|
|
126
|
+
# A node that raises shows why in its place, rather than stopping the page. A node that renders its own HTML can take
|
|
127
|
+
# written:, its block's lines as they're written in the file, like a grid: drawn from them.
|
|
128
|
+
def node_markdown(block, context, written:)
|
|
129
|
+
return Nodes.rendered(block[:node], context, written:) if !block[:writes] || Nodes.html?(block[:node])
|
|
125
130
|
|
|
126
131
|
Nodes.call(block[:node], :rewrite, **context.to_h).to_s
|
|
127
132
|
rescue StandardError => e
|
|
@@ -133,10 +138,13 @@ module Raindown
|
|
|
133
138
|
end
|
|
134
139
|
|
|
135
140
|
def span_at(lines, index, bare:)
|
|
141
|
+
return bare_grid(lines, index) if bare && lines[index].match?(BARE_GRID)
|
|
142
|
+
|
|
136
143
|
found, pattern = directive(lines[index], bare:)
|
|
137
144
|
return unless found && (klass = Nodes.node_class(found[2]))
|
|
138
145
|
|
|
139
|
-
span = { name: found[2], class: klass, args: found[3].to_s, antlers: %i[open self_closing].include?(pattern)
|
|
146
|
+
span = { name: found[2], class: klass, args: found[3].to_s, antlers: %i[open self_closing].include?(pattern),
|
|
147
|
+
first: index + 1 }
|
|
140
148
|
case pattern
|
|
141
149
|
when :open, :comment_open
|
|
142
150
|
end_tag(lines, index, found[2], comment: pattern == :comment_open)
|
|
@@ -159,6 +167,11 @@ module Raindown
|
|
|
159
167
|
nil
|
|
160
168
|
end
|
|
161
169
|
|
|
170
|
+
def bare_grid(lines, index)
|
|
171
|
+
grid = { name: 'grid', class: Nodes.node_class('grid'), args: '', antlers: false }
|
|
172
|
+
{ **bare_block(lines, index), first: index, writes: true, **grid }
|
|
173
|
+
end
|
|
174
|
+
|
|
162
175
|
# A bare block holds the lines straight below it, up to the next blank line, with fenced code kept whole.
|
|
163
176
|
def bare_block(lines, open) = { open:, close: nil, last: CodeBlocks.last_before_blank(lines, open) }
|
|
164
177
|
|