markly 0.1.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3121bb14bcb09883bfb84879b6b69e28d47e3781f0bcd6225737734040ac2ec5
4
- data.tar.gz: 2f47e7add1ee43c03e7b75b45e5f2c7ec2c322cac842aa5f5bdb8e539fbca8d5
3
+ metadata.gz: 770b81ed57426a591eaa4335ad913df94f61017e08f5e19872766e59ec3d33db
4
+ data.tar.gz: 45f5295c1fae1747c841a85b8f17f1442d47ad91194a38874e1313e8c95313f4
5
5
  SHA512:
6
- metadata.gz: 6da8e6ef6f672489b7bcd4b6e378c86d3240e4b3f9d67aaba26bbe5414a363c4a071be3695bca85bb9658c0213cf1e59d32edb389b5ec8a0aebd9689a8245ff7
7
- data.tar.gz: ce28eac1fcca2c0dfc6e637ca937e16ffbb211d4d64c596f3869b56860fb04227b20065c86a5d335cb8d11cdd49c1e924a09b0caa0b515e404e123420ed39e7d
6
+ metadata.gz: 75bdb5b83d6ca7685fb659d27f45bfaaefe1241f306ec21281bbbd5906612663c8e4ba7ad3abf59069ff6501c72933c6770d09176dd119de946e5baea0b826ff
7
+ data.tar.gz: 0c0aa153efcbbb7914a805e52bf80fdda4bdc5d1ddb0352f7bb00c334f2730d8cd342a19fa273082c07597c16166c666b8f86be66a197729f902e0539903af8f
@@ -41,11 +41,15 @@ static VALUE sym_right;
41
41
  static VALUE sym_center;
42
42
 
43
43
  static void rb_Markly_Node_free(void *data) {
44
- // It's important that the `free` function does not inspect the node data, as it may be part of a tree that was already freed.
45
- cmark_node *node = (cmark_node*)data;
46
-
47
- if (cmark_node_parent(node) == NULL) {
48
- cmark_node_free(node);
44
+ // If a parent of this node is already freed, `rb_Markly_Node_freed` will ensure all the nodes are nullified.
45
+ if (data) {
46
+ cmark_node *node = (cmark_node*)data;
47
+
48
+ if (cmark_node_parent(node) == NULL) {
49
+ cmark_node_free(node);
50
+ } else {
51
+ cmark_node_set_user_data(node, NULL);
52
+ }
49
53
  }
50
54
  }
51
55
 
@@ -79,6 +83,12 @@ static const rb_data_type_t rb_Markly_Node_Type = {
79
83
  .flags = RUBY_TYPED_FREE_IMMEDIATELY,
80
84
  };
81
85
 
86
+ static void rb_Markly_Node_freed(cmark_mem *mem, void *user_data) {
87
+ VALUE self = (VALUE)user_data;
88
+
89
+ RTYPEDDATA_DATA(self) = NULL;
90
+ }
91
+
82
92
  static VALUE rb_Markly_Node_wrap(cmark_node *node) {
83
93
  if (node == NULL)
84
94
  return Qnil;
@@ -91,6 +101,7 @@ static VALUE rb_Markly_Node_wrap(cmark_node *node) {
91
101
 
92
102
  VALUE self = TypedData_Wrap_Struct(rb_Markly_Node, &rb_Markly_Node_Type, node);
93
103
  cmark_node_set_user_data(node, (void *)self);
104
+ cmark_node_set_user_data_free_func(node, rb_Markly_Node_freed);
94
105
 
95
106
  return self;
96
107
  }
@@ -121,16 +132,6 @@ static const rb_data_type_t rb_Markly_Parser_Type = {
121
132
  .flags = RUBY_TYPED_FREE_IMMEDIATELY,
122
133
  };
123
134
 
124
- static VALUE rb_Markly_Parser_root(VALUE self) {
125
- cmark_parser *parser = NULL;
126
-
127
- TypedData_Get_Struct(self, cmark_parser, &rb_Markly_Parser_Type, parser);
128
-
129
- assert(parser != NULL);
130
-
131
- return rb_Markly_Node_wrap(parser->root);
132
- }
133
-
134
135
  static VALUE rb_Markly_Parser_alloc(VALUE self) {
135
136
  return TypedData_Wrap_Struct(self, &rb_Markly_Parser_Type, NULL);
136
137
  }
@@ -262,6 +263,21 @@ static VALUE rb_node_new(VALUE self, VALUE type) {
262
263
  return rb_Markly_Node_wrap(node);
263
264
  }
264
265
 
266
+ static VALUE rb_node_replace(VALUE self, VALUE other) {
267
+ cmark_node *current_node = NULL, *replacement_node = NULL;
268
+
269
+ TypedData_Get_Struct(self, cmark_node, &rb_Markly_Node_Type, current_node);
270
+ TypedData_Get_Struct(other, cmark_node, &rb_Markly_Node_Type, replacement_node);
271
+
272
+ int result = cmark_node_replace(current_node, replacement_node);
273
+
274
+ if (result == 0) {
275
+ rb_raise(rb_Markly_Error, "could not replace node");
276
+ }
277
+
278
+ return other;
279
+ }
280
+
265
281
  static VALUE encode_utf8_string(const char *c_string) {
266
282
  VALUE string = rb_str_new2(c_string);
267
283
  int enc = rb_enc_find_index("UTF-8");
@@ -315,10 +331,10 @@ static VALUE rb_node_set_string_content(VALUE self, VALUE s) {
315
331
  * Returns a {Symbol} representing the node's type.
316
332
  */
317
333
  static VALUE rb_node_get_type(VALUE self) {
318
- int node_type;
319
- cmark_node *node;
320
- VALUE symbol;
321
- const char *s;
334
+ int node_type = 0;
335
+ cmark_node *node = NULL;
336
+ VALUE symbol = NULL;
337
+ const char *s = NULL;
322
338
 
323
339
  TypedData_Get_Struct(self, cmark_node, &rb_Markly_Node_Type, node);
324
340
 
@@ -1178,10 +1194,12 @@ __attribute__((visibility("default"))) void Init_markly() {
1178
1194
  rb_define_method(rb_Markly_Parser, "initialize", rb_Markly_Parser_initialize, 1);
1179
1195
  rb_define_method(rb_Markly_Parser, "enable", rb_Markly_Parser_enable, 1);
1180
1196
  rb_define_method(rb_Markly_Parser, "parse", rb_Markly_Parser_parse, 1);
1181
- rb_define_method(rb_Markly_Parser, "root", rb_Markly_Parser_root, 0);
1182
1197
 
1183
1198
  rb_Markly_Node = rb_define_class_under(rb_Markly, "Node", rb_cObject);
1184
1199
  rb_define_singleton_method(rb_Markly_Node, "new", rb_node_new, 1);
1200
+
1201
+ rb_define_method(rb_Markly_Node, "replace", rb_node_replace, 1);
1202
+
1185
1203
  rb_define_method(rb_Markly_Node, "string_content", rb_node_get_string_content, 0);
1186
1204
  rb_define_method(rb_Markly_Node, "string_content=", rb_node_set_string_content, 1);
1187
1205
  rb_define_method(rb_Markly_Node, "type", rb_node_get_type, 0);
Binary file
@@ -39,6 +39,8 @@ module Markly
39
39
  _render_commonmark(flags, width).force_encoding('utf-8')
40
40
  end
41
41
 
42
+ alias to_markdown to_commonmark
43
+
42
44
  # Public: Convert the node to a plain text string.
43
45
  #
44
46
  # options - A {Symbol} or {Array of Symbol}s indicating the render options
@@ -1,15 +1,31 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'cgi'
4
+
3
5
  module Markly
4
- class HtmlRenderer < Renderer
6
+ class HTMLRenderer < Renderer
7
+ def initialize(ids: false, **options)
8
+ super(**options)
9
+
10
+ @ids = ids
11
+ end
12
+
5
13
  def document(_)
6
14
  super
7
15
  out("</ol>\n</section>\n") if @written_footnote_ix
8
16
  end
9
17
 
18
+ def id_for(node)
19
+ if @ids
20
+ id = node.to_plaintext.chomp.downcase.gsub(/\s+/, '-')
21
+
22
+ return " id=\"#{CGI.escape_html id}\""
23
+ end
24
+ end
25
+
10
26
  def header(node)
11
27
  block do
12
- out('<h', node.header_level, "#{source_position(node)}>", :children,
28
+ out('<h', node.header_level, "#{id_for(node)}#{source_position(node)}>", :children,
13
29
  '</h', node.header_level, '>')
14
30
  end
15
31
  end
@@ -249,4 +265,7 @@ module Markly
249
265
  node.tasklist_item_checked?
250
266
  end
251
267
  end
268
+
269
+ # Legacy.
270
+ HtmlRenderer = HTMLRenderer
252
271
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Markly
4
- VERSION = '0.1.0'
4
+ VERSION = '0.4.0'
5
5
  end
metadata CHANGED
@@ -1,16 +1,16 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: markly
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Garen Torikian
8
8
  - Ashe Connor
9
9
  - Samuel Williams
10
- autorequire:
10
+ autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2020-05-24 00:00:00.000000000 Z
13
+ date: 2020-07-13 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: bake
@@ -26,34 +26,6 @@ dependencies:
26
26
  - - ">="
27
27
  - !ruby/object:Gem::Version
28
28
  version: '0'
29
- - !ruby/object:Gem::Dependency
30
- name: bake-bundler
31
- requirement: !ruby/object:Gem::Requirement
32
- requirements:
33
- - - ">="
34
- - !ruby/object:Gem::Version
35
- version: '0'
36
- type: :development
37
- prerelease: false
38
- version_requirements: !ruby/object:Gem::Requirement
39
- requirements:
40
- - - ">="
41
- - !ruby/object:Gem::Version
42
- version: '0'
43
- - !ruby/object:Gem::Dependency
44
- name: bake-modernize
45
- requirement: !ruby/object:Gem::Requirement
46
- requirements:
47
- - - ">="
48
- - !ruby/object:Gem::Version
49
- version: '0'
50
- type: :development
51
- prerelease: false
52
- version_requirements: !ruby/object:Gem::Requirement
53
- requirements:
54
- - - ">="
55
- - !ruby/object:Gem::Version
56
- version: '0'
57
29
  - !ruby/object:Gem::Dependency
58
30
  name: minitest
59
31
  requirement: !ruby/object:Gem::Requirement
@@ -96,8 +68,8 @@ dependencies:
96
68
  - - "~>"
97
69
  - !ruby/object:Gem::Version
98
70
  version: '0.9'
99
- description:
100
- email:
71
+ description:
72
+ email:
101
73
  executables:
102
74
  - markly
103
75
  extensions:
@@ -178,7 +150,7 @@ files:
178
150
  - ext/markly/xml.c
179
151
  - lib/markly.rb
180
152
  - lib/markly/flags.rb
181
- - lib/markly/markly.so
153
+ - lib/markly/markly.bundle
182
154
  - lib/markly/node.rb
183
155
  - lib/markly/node/inspect.rb
184
156
  - lib/markly/renderer.rb
@@ -189,7 +161,7 @@ licenses:
189
161
  - MIT
190
162
  metadata:
191
163
  funding_uri: https://github.com/sponsors/ioquatix/
192
- post_install_message:
164
+ post_install_message:
193
165
  rdoc_options: []
194
166
  require_paths:
195
167
  - lib
@@ -205,7 +177,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
205
177
  version: '0'
206
178
  requirements: []
207
179
  rubygems_version: 3.1.2
208
- signing_key:
180
+ signing_key:
209
181
  specification_version: 4
210
182
  summary: CommonMark parser and renderer. Written in C, wrapped in Ruby.
211
183
  test_files: []
Binary file