djot 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,75 @@
1
+ local make_match, unpack_match
2
+
3
+ if jit or not string.pack then
4
+ -- for luajit or lua 5.1, we don't have string.pack/unpack, so we use arrays.
5
+ -- This is faster than using ffi to pack things in C structs.
6
+
7
+ make_match = function(startpos, endpos, annotation)
8
+ return {startpos, endpos, annotation}
9
+ end
10
+
11
+ unpack_match = unpack
12
+
13
+ else
14
+ -- for standard lua >= 5.2, we use string.pack/unpack which gives a
15
+ -- more memory-efficient representation than arrays.
16
+
17
+ make_match = function(startpos, endpos, annotation)
18
+ return string.pack("=I4I4z", startpos, endpos, annotation)
19
+ end
20
+
21
+ unpack_match = function(match)
22
+ local startpos, endpos, annotation = string.unpack("=I4I4z", match)
23
+ return startpos, endpos, annotation
24
+ end
25
+ end
26
+
27
+ local get_length = function(match)
28
+ local startpos, endpos = unpack_match(match)
29
+ return 1 + (endpos - startpos)
30
+ end
31
+
32
+ local format_match = function(match)
33
+ local startpos, endpos, annotation = unpack_match(match)
34
+ return string.format("%-s %d-%d\n", annotation, startpos, endpos)
35
+ end
36
+
37
+ local function matches_pattern(match, patt)
38
+ if match then
39
+ local _, _, annot = unpack_match(match)
40
+ return string.find(annot, patt)
41
+ end
42
+ end
43
+
44
+ return {
45
+ make_match = make_match,
46
+ unpack_match = unpack_match,
47
+ get_length = get_length,
48
+ format_match = format_match,
49
+ matches_pattern = matches_pattern
50
+ }
51
+
52
+
53
+ --[[
54
+ Copyright (C) 2022 John MacFarlane
55
+
56
+ Permission is hereby granted, free of charge, to any person obtaining
57
+ a copy of this software and associated documentation files (the
58
+ "Software"), to deal in the Software without restriction, including
59
+ without limitation the rights to use, copy, modify, merge, publish,
60
+ distribute, sublicense, and/or sell copies of the Software, and to
61
+ permit persons to whom the Software is furnished to do so, subject to
62
+ the following conditions:
63
+
64
+ The above copyright notice and this permission notice shall be included
65
+ in all copies or substantial portions of the Software.
66
+
67
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
68
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
69
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
70
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
71
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
72
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
73
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
74
+
75
+ ]]
data/lib/lua/djot.lua ADDED
@@ -0,0 +1,107 @@
1
+ local block = require("djot.block")
2
+ local ast = require("djot.ast")
3
+ local html = require("djot.html")
4
+ local match = require("djot.match")
5
+
6
+ local format_match = match.format_match
7
+
8
+ local StringHandle = {}
9
+
10
+ function StringHandle:new()
11
+ local buffer = {}
12
+ setmetatable(buffer, StringHandle)
13
+ StringHandle.__index = StringHandle
14
+ return buffer
15
+ end
16
+
17
+ function StringHandle:write(s)
18
+ self[#self + 1] = s
19
+ end
20
+
21
+ function StringHandle:flush()
22
+ local result = table.concat(self)
23
+ self = {}
24
+ return result
25
+ end
26
+
27
+ local Parser = block.Parser
28
+
29
+ function Parser:issue_warnings(handle)
30
+ if self.opts.verbose then
31
+ local warnings = self.warnings
32
+ for i=1,#warnings do
33
+ handle:write(string.format("Warning: %s at byte position %d\n",
34
+ warnings[i][2], warnings[i][1]))
35
+ end
36
+ end
37
+ end
38
+
39
+ function Parser:render_matches(handle)
40
+ if not handle then
41
+ handle = StringHandle:new()
42
+ end
43
+ local matches = self:get_matches()
44
+ self:issue_warnings(io.stderr)
45
+ for i=1,#matches do
46
+ handle:write(format_match(matches[i]))
47
+ end
48
+ return handle:flush()
49
+ end
50
+
51
+ function Parser:build_ast()
52
+ self.ast = ast.to_ast(self.subject, self.matches, self.opts)
53
+ end
54
+
55
+ function Parser:render_ast(handle)
56
+ if not handle then
57
+ handle = StringHandle:new()
58
+ end
59
+ if not self.ast then
60
+ self:build_ast()
61
+ end
62
+ self:issue_warnings(io.stderr)
63
+ ast.render(self.ast, handle)
64
+ return handle:flush()
65
+ end
66
+
67
+ function Parser:render_html(handle)
68
+ if not handle then
69
+ handle = StringHandle:new()
70
+ end
71
+ if not self.ast then
72
+ self:build_ast()
73
+ end
74
+ self:issue_warnings(io.stderr)
75
+ local renderer = html.Renderer:new()
76
+ renderer:render(self.ast, handle)
77
+ return handle:flush()
78
+ end
79
+
80
+ return {
81
+ Parser = Parser
82
+ }
83
+
84
+
85
+ --[[
86
+ Copyright (C) 2022 John MacFarlane
87
+
88
+ Permission is hereby granted, free of charge, to any person obtaining
89
+ a copy of this software and associated documentation files (the
90
+ "Software"), to deal in the Software without restriction, including
91
+ without limitation the rights to use, copy, modify, merge, publish,
92
+ distribute, sublicense, and/or sell copies of the Software, and to
93
+ permit persons to whom the Software is furnished to do so, subject to
94
+ the following conditions:
95
+
96
+ The above copyright notice and this permission notice shall be included
97
+ in all copies or substantial portions of the Software.
98
+
99
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
100
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
101
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
102
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
103
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
104
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
105
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
106
+
107
+ ]]
data/sig/djot.rbs ADDED
@@ -0,0 +1,6 @@
1
+ module Djot
2
+ VERSION: String
3
+ def self.render_html: (String) -> String
4
+ def self.render_matches: (String) -> String
5
+ def self.render_ast: (String) -> String
6
+ end
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: djot
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - gemmaro
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-09-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: ruby-lua
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.4'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.4'
27
+ description: Call Lua Djot for rendering Djot documents in HTML
28
+ email:
29
+ - gemmaro.dev@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".rubocop.yml"
35
+ - CHANGELOG.md
36
+ - CODE_OF_CONDUCT.md
37
+ - Gemfile
38
+ - LICENSE.txt
39
+ - README.md
40
+ - Rakefile
41
+ - Steepfile
42
+ - djot.gemspec
43
+ - lib/djot.rb
44
+ - lib/djot/version.rb
45
+ - lib/lua/djot.lua
46
+ - lib/lua/djot/ast.lua
47
+ - lib/lua/djot/attributes.lua
48
+ - lib/lua/djot/block.lua
49
+ - lib/lua/djot/emoji.lua
50
+ - lib/lua/djot/html.lua
51
+ - lib/lua/djot/inline.lua
52
+ - lib/lua/djot/match.lua
53
+ - sig/djot.rbs
54
+ homepage: https://gitlab.com/gemmaro/djot
55
+ licenses:
56
+ - MIT
57
+ metadata:
58
+ homepage_uri: https://gitlab.com/gemmaro/djot
59
+ source_code_uri: https://gitlab.com/gemmaro/djot
60
+ changelog_uri: https://gitlab.com/gemmaro/djot/blob/main/CHANGELOG.md
61
+ rubygems_mfa_required: 'true'
62
+ post_install_message:
63
+ rdoc_options: []
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: 2.6.0
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ requirements: []
77
+ rubygems_version: 3.3.7
78
+ signing_key:
79
+ specification_version: 4
80
+ summary: Call Lua Djot
81
+ test_files: []