Class: Greenmat::Markdown

Inherits:
Object
  • Object
show all
Defined in:
lib/greenmat.rb,
ext/greenmat/gm_markdown.c

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#rendererObject (readonly)

Returns the value of attribute renderer.



7
8
9
# File 'lib/greenmat.rb', line 7

def renderer
  @renderer
end

Class Method Details

.new(*args) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'ext/greenmat/gm_markdown.c', line 96

static VALUE rb_greenmat_md__new(int argc, VALUE *argv, VALUE klass)
{
	VALUE rb_markdown, rb_rndr, hash, rndr_options;
	unsigned int extensions = 0;

	struct rb_greenmat_rndr *rndr;
	struct sd_markdown *markdown;

	if (rb_scan_args(argc, argv, "11", &rb_rndr, &hash) == 2)
		rb_greenmat_md_flags(hash, &extensions);

	if (rb_obj_is_kind_of(rb_rndr, rb_cClass))
		rb_rndr = rb_funcall(rb_rndr, rb_intern("new"), 0);

	if (!rb_obj_is_kind_of(rb_rndr, rb_cRenderBase))
		rb_raise(rb_eTypeError, "Invalid Renderer instance given");

	/**
	 * Automatically enable the `fenced_code_blocks` option if
	 * given a kind of `HTML_TOC` object since many languages
	 * like Ruby use the sharp to comment code so these comments
	 * would be processed as titles.
	 */
	if (rb_obj_is_kind_of(rb_rndr, rb_cRenderHTML_TOC))
		extensions |= MKDEXT_FENCED_CODE;

	Data_Get_Struct(rb_rndr, struct rb_greenmat_rndr, rndr);

	/* Merge the current options in the @options hash */
	if (hash != Qnil) {
		rndr_options = rb_funcall(rb_iv_get(rb_rndr, "@options"), rb_intern("merge"), 1, hash);
		rb_iv_set(rb_rndr, "@options", rndr_options);
	}

	markdown = sd_markdown_new(extensions, MAX_NESTING, &rndr->callbacks, &rndr->options);
	if (!markdown)
		rb_raise(rb_eRuntimeError, "Failed to create new Renderer class");

	rb_markdown = Data_Wrap_Struct(klass, NULL, rb_greenmat_md__free, markdown);
	rb_iv_set(rb_markdown, "@renderer", rb_rndr);

	return rb_markdown;
}

Instance Method Details

#render(text) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'ext/greenmat/gm_markdown.c', line 140

static VALUE rb_greenmat_md_render(VALUE self, VALUE text)
{
	VALUE rb_rndr;
	struct buf *output_buf;
	struct sd_markdown *markdown;

	Check_Type(text, T_STRING);

	rb_rndr = rb_iv_get(self, "@renderer");
	Data_Get_Struct(self, struct sd_markdown, markdown);

	if (rb_respond_to(rb_rndr, rb_intern("preprocess")))
		text = rb_funcall(rb_rndr, rb_intern("preprocess"), 1, text);
	if (NIL_P(text))
		return Qnil;

#ifdef HAVE_RUBY_ENCODING_H
	{
		struct rb_greenmat_rndr *renderer;
		Data_Get_Struct(rb_rndr, struct rb_greenmat_rndr, renderer);
		renderer->options.active_enc = rb_enc_get(text);
	}
#endif

	/* initialize buffers */
	output_buf = bufnew(128);

	/* render the magic */
	sd_markdown_render(
		output_buf,
		(const uint8_t*)RSTRING_PTR(text),
		RSTRING_LEN(text),
		markdown);

	/* build the Ruby string */
	text = rb_enc_str_new((const char*)output_buf->data, output_buf->size, rb_enc_get(text));

	bufrelease(output_buf);

	if (rb_respond_to(rb_rndr, rb_intern("postprocess")))
		text = rb_funcall(rb_rndr, rb_intern("postprocess"), 1, text);

	return text;
}