File: parser_spec.rb

package info (click to toggle)
ruby-org 0.9.12-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,852 kB
  • sloc: ruby: 3,044; lisp: 50; makefile: 4
file content (492 lines) | stat: -rw-r--r-- 20,437 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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
139
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
require 'spec_helper'
require 'pathname'

describe Orgmode::Parser do
  it "should open ORG files" do
    parser = Orgmode::Parser.load(RememberFile)
  end

  it "should fail on non-existant files" do
    expect { parser = Orgmode::Parser.load("does-not-exist.org") }.to raise_error
  end

  it "should load all of the lines" do
    parser = Orgmode::Parser.load(RememberFile)
    expect(parser.lines.length).to eql(53)
  end

  it "should find all headlines" do
    parser = Orgmode::Parser.load(RememberFile)
    expect(parser.headlines.count).to eq(12)
  end

  it "can find a headline by index" do
    parser = Orgmode::Parser.load(RememberFile)
    line = parser.headlines[1].to_s
    expect(line).to eql("** YAML header in Webby\n")
  end

  it "should determine headline levels" do
    parser = Orgmode::Parser.load(RememberFile)
    expect(parser.headlines[0].level).to eql(1)
    expect(parser.headlines[1].level).to eql(2)
  end

  it "should include the property drawer items from a headline" do
    parser = Orgmode::Parser.load(FreeformExampleFile)
    expect(parser.headlines.first.property_drawer.count).to eq(2)
    expect(parser.headlines.first.property_drawer['DATE']).to eq('2009-11-26')
    expect(parser.headlines.first.property_drawer['SLUG']).to eq('future-ideas')
  end

  it "should put body lines in headlines" do
    parser = Orgmode::Parser.load(RememberFile)
    expect(parser.headlines[0].body_lines.count).to eq(1)
    expect(parser.headlines[1].body_lines.count).to eq(7)
  end

  it "should understand lines before the first headline" do
    parser = Orgmode::Parser.load(FreeformFile)
    expect(parser.header_lines.count).to eq(22)
  end

  it "should load in-buffer settings" do
    parser = Orgmode::Parser.load(FreeformFile)
    expect(parser.in_buffer_settings.count).to eq(12)
    expect(parser.in_buffer_settings["TITLE"]).to eql("Freeform")
    expect(parser.in_buffer_settings["EMAIL"]).to eql("bdewey@gmail.com")
    expect(parser.in_buffer_settings["LANGUAGE"]).to eql("en")
  end

  it "should understand OPTIONS" do
    parser = Orgmode::Parser.load(FreeformFile)
    expect(parser.options.count).to        eq(33)
    expect(parser.options["TeX"]).to       eql("t")
    expect(parser.options["todo"]).to      eql("t")
    expect(parser.options["\\n"]).to       eql("nil")
    expect(parser.options['H']).to         eq('3')
    expect(parser.options['num']).to       eq('t')
    expect(parser.options['toc']).to       eq('nil')
    expect(parser.options['\n']).to        eq('nil')
    expect(parser.options['@']).to         eq('t')
    expect(parser.options[':']).to         eq('t')
    expect(parser.options['|']).to         eq('t')
    expect(parser.options['^']).to         eq('t')
    expect(parser.options['-']).to         eq('t')
    expect(parser.options['f']).to         eq('t')
    expect(parser.options['*']).to         eq('t')
    expect(parser.options['<']).to         eq('t')
    expect(parser.options['TeX']).to       eq('t')
    expect(parser.options['LaTeX']).to     eq('nil')
    expect(parser.options['skip']).to      eq('nil')
    expect(parser.options['todo']).to      eq('t')
    expect(parser.options['pri']).to       eq('nil')
    expect(parser.options['tags']).to      eq('not-in-toc')
    expect(parser.options["'"]).to         eq('t')
    expect(parser.options['arch']).to      eq('headline')
    expect(parser.options['author']).to    eq('t')
    expect(parser.options['c']).to         eq('nil')
    expect(parser.options['creator']).to   eq('comment')
    expect(parser.options['d']).to         eq('(not LOGBOOK)')
    expect(parser.options['date']).to      eq('t')
    expect(parser.options['e']).to         eq('t')
    expect(parser.options['email']).to     eq('nil')
    expect(parser.options['inline']).to    eq('t')
    expect(parser.options['p']).to         eq('nil')
    expect(parser.options['stat']).to      eq('t')
    expect(parser.options['tasks']).to     eq('t')
    expect(parser.options['tex']).to       eq('t')
    expect(parser.options['timestamp']).to eq('t')

    expect(parser.export_todo?).to be true
    parser.options.delete("todo")
    expect(parser.export_todo?).to be false
  end

  it "should skip in-buffer settings inside EXAMPLE blocks" do
    parser = Orgmode::Parser.load(FreeformExampleFile)
    expect(parser.in_buffer_settings.count).to eq(0)
  end

  it "should return a textile string" do
    parser = Orgmode::Parser.load(FreeformFile)
    expect(parser.to_textile).to be_kind_of(String)
  end

  it "should understand export table option" do
    fname = File.join(File.dirname(__FILE__), %w[html_examples skip-table.org])
    data = IO.read(fname)
    p = Orgmode::Parser.new(data)
    expect(p.export_tables?).to be false
  end

  it "should add code block name as a line property" do
    example = <<EXAMPLE
* Sample

#+name: hello_world
#+begin_src sh :results output
echo 'hello world'
#+end_src
EXAMPLE
    o = Orgmode::Parser.new(example)
    h = o.headlines.first
    line = h.body_lines.find { |l| l.to_s == "#+begin_src sh :results output"}
    expect(line.properties['block_name']).to eq('hello_world')
  end

  context "with a table that begins with a separator line" do
    let(:parser) { Orgmode::Parser.new(data) }
    let(:data) { Pathname.new(File.dirname(__FILE__)).join('data', 'tables.org').read }

    it "should parse without errors" do
      expect(parser.headlines.size).to eq(2)
    end
  end

  describe "Custom keyword parser" do
    fname = File.join(File.dirname(__FILE__), %w[html_examples custom-todo.org])
    p = Orgmode::Parser.load(fname)
    valid_keywords = %w[TODO INPROGRESS WAITING DONE CANCELED]
    invalid_keywords = %w[TODOX todo inprogress Waiting done cANCELED NEXT |]
    valid_keywords.each do |kw|
      it "should match custom keyword #{kw}" do
        expect(kw =~ p.custom_keyword_regexp).to be_truthy
      end
    end
    invalid_keywords.each do |kw|
      it "should not match custom keyword #{kw}" do
        expect((kw =~ p.custom_keyword_regexp)).to be_nil
      end
    end
    it "should not match blank as a custom keyword" do
      expect(("" =~ p.custom_keyword_regexp)).to be_nil
    end
  end

  describe "Custom include/exclude parser" do
    fname = File.join(File.dirname(__FILE__), %w[html_examples export-tags.org])
    p = Orgmode::Parser.load(fname)
    it "should load tags" do
      expect(p.export_exclude_tags.count).to eq(2)
      expect(p.export_select_tags.count).to eq(1)
    end
  end

  describe "Export to Textile test cases" do
    data_directory = File.join(File.dirname(__FILE__), "textile_examples")
    org_files = File.expand_path(File.join(data_directory, "*.org" ))
    files = Dir.glob(org_files)
    files.each do |file|
      basename = File.basename(file, ".org")
      textile_name = File.join(data_directory, basename + ".textile")
      textile_name = File.expand_path(textile_name)

      it "should convert #{basename}.org to Textile" do
        expected = IO.read(textile_name)
        expect(expected).to be_kind_of(String)
        parser = Orgmode::Parser.new(IO.read(file))
        actual = parser.to_textile
        expect(actual).to be_kind_of(String)
        expect(actual).to eq(expected)
      end
    end
  end

  describe "Make it possible to disable rubypants pass" do
    it "should allow the raw dash" do
      org = "This is a dash -- that will remain as is."
      parser = Orgmode::Parser.new(org, { :skip_rubypants_pass => true })
      expected = "<p>#{org}</p>\n"
      expect(expected).to eq(parser.to_html)
    end
  end

  describe "Export to HTML test cases" do
    # Dynamic generation of examples from each *.org file in html_examples.
    # Each of these files is convertable to HTML.
    data_directory = File.join(File.dirname(__FILE__), "html_examples")
    org_files = File.expand_path(File.join(data_directory, "*.org" ))
    files = Dir.glob(org_files)
    files.each do |file|
      basename = File.basename(file, ".org")
      textile_name = File.join(data_directory, basename + ".html")
      textile_name = File.expand_path(textile_name)

      it "should convert #{basename}.org to HTML" do
        expected = IO.read(textile_name)
        expect(expected).to be_kind_of(String)
        parser = Orgmode::Parser.new(IO.read(file), { :allow_include_files => true })
        actual = parser.to_html
        expect(actual).to be_kind_of(String)
        expect(actual).to eq(expected)
      end

      it "should render #{basename}.org to HTML using Tilt templates" do
        ENV['ORG_RUBY_ENABLE_INCLUDE_FILES'] = 'true'
        expected = IO.read(textile_name)
        template = Tilt.new(file).render
        expect(template).to eq(expected)
        ENV['ORG_RUBY_ENABLE_INCLUDE_FILES'] = ''
      end
    end

    it "should not render #+INCLUDE directive when explicitly indicated" do
      data_directory = File.join(File.dirname(__FILE__), "html_examples")
      expected = File.read(File.join(data_directory, "include-file-disabled.html"))
      org_file = File.join(data_directory, "include-file.org")
      parser = Orgmode::Parser.new(IO.read(org_file), :allow_include_files => false)
      actual = parser.to_html
      expect(actual).to eq(expected)
    end

    it "should render #+INCLUDE when ORG_RUBY_INCLUDE_ROOT is set" do
      data_directory = File.expand_path(File.join(File.dirname(__FILE__), "html_examples"))
      ENV['ORG_RUBY_INCLUDE_ROOT'] = data_directory
      expected = File.read(File.join(data_directory, "include-file.html"))
      org_file = File.join(data_directory, "include-file.org")
      parser = Orgmode::Parser.new(IO.read(org_file))
      actual = parser.to_html
      expect(actual).to eq(expected)
      ENV['ORG_RUBY_INCLUDE_ROOT'] = nil
    end
  end

  describe "Export to HTML test cases with code syntax highlight disabled" do
    code_syntax_examples_directory = File.join(File.dirname(__FILE__), "html_code_syntax_highlight_examples")

    # Do not use syntax coloring for source code blocks
    org_files = File.expand_path(File.join(code_syntax_examples_directory, "*-no-color.org"))
    files = Dir.glob(org_files)

    files.each do |file|
      basename = File.basename(file, ".org")
      org_filename = File.join(code_syntax_examples_directory, basename + ".html")
      org_filename = File.expand_path(org_filename)

      it "should convert #{basename}.org to HTML" do
        expected = IO.read(org_filename)
        expect(expected).to be_kind_of(String)
        parser = Orgmode::Parser.new(IO.read(file), {
                                       :allow_include_files   => true,
                                       :skip_syntax_highlight => true
                                     })
        actual = parser.to_html
        expect(actual).to be_kind_of(String)
        expect(actual).to eq(expected)
      end

      it "should render #{basename}.org to HTML using Tilt templates",
      :if => (defined? Coderay or defined? Pygments) do
        ENV['ORG_RUBY_ENABLE_INCLUDE_FILES'] = 'true'
        expected = IO.read(org_filename)
        template = Tilt.new(file).render
        expect(template).to eq(expected)
        ENV['ORG_RUBY_ENABLE_INCLUDE_FILES'] = ''
      end
    end
  end

  ['coderay', 'pygments'].each do |highlighter|
    if defined? (instance_eval highlighter.capitalize)
      describe "Export to HTML test cases with code syntax highlight: #{highlighter}" do
        code_syntax_examples_directory = File.join(File.dirname(__FILE__), "html_code_syntax_highlight_examples")
        files = []

        # Either Pygments or Coderay
        begin
          require highlighter
        rescue LoadError
          next
        end

        org_files = File.expand_path(File.join(code_syntax_examples_directory, "*-#{highlighter}.org"))
        files = Dir.glob(org_files)

        files.each do |file|
          basename = File.basename(file, ".org")
          org_filename = File.join(code_syntax_examples_directory, basename + ".html")
          org_filename = File.expand_path(org_filename)

          it "should convert #{basename}.org to HTML" do
            expected = IO.read(org_filename)
            expect(expected).to be_kind_of(String)
            parser = Orgmode::Parser.new(IO.read(file), :allow_include_files => true)
            actual = parser.to_html
            expect(actual).to be_kind_of(String)
            expect(actual).to eq(expected)
          end

          it "should render #{basename}.org to HTML using Tilt templates" do
            ENV['ORG_RUBY_ENABLE_INCLUDE_FILES'] = 'true'
            expected = IO.read(org_filename)
            template = Tilt.new(file).render
            expect(template).to eq(expected)
            ENV['ORG_RUBY_ENABLE_INCLUDE_FILES'] = ''
          end
        end
      end
    end
  end

  describe "Export to Markdown test cases" do
    data_directory = File.join(File.dirname(__FILE__), "markdown_examples")
    org_files = File.expand_path(File.join(data_directory, "*.org" ))
    files = Dir.glob(org_files)
    files.each do |file|
      basename = File.basename(file, ".org")
      markdown_name = File.join(data_directory, basename + ".md")
      markdown_name = File.expand_path(markdown_name)

      it "should convert #{basename}.org to Markdown" do
        expected = IO.read(markdown_name)
        expect(expected).to be_kind_of(String)
        parser = Orgmode::Parser.new(IO.read(file), :allow_include_files => false)
        actual = parser.to_markdown
        expect(actual).to be_kind_of(String)
        expect(actual).to eq(expected)
      end
    end
  end

  describe "Export to Markdown with incorrect custom markup test cases" do
    # The following tests export Markdown to the default markup of org-ruby
    # since the YAML file only contains the incorrect keys
    data_directory = File.join(File.dirname(__FILE__), "markdown_with_custom_markup_examples")
    org_files = File.expand_path(File.join(data_directory, "*.org" ))
    files = Dir.glob(org_files)
    files.each do |file|
      basename = File.basename(file, ".org")
      default_html_name = File.join(data_directory, basename + "_default.md")
      default_html_name = File.expand_path(default_html_name)
      custom_markup_file = File.join(data_directory, "incorrect_markup_for_markdown.yml")
      custom_markup_file = File.expand_path(custom_markup_file)

      it "should convert #{basename}.org to Markdown with the default markup" do
        expected = IO.read(default_html_name)
        expect(expected).to be_kind_of(String)
        parser = Orgmode::Parser.new(IO.read(file), { :allow_include_files => true, :markup_file => custom_markup_file })
        actual = parser.to_markdown
        expect(actual).to be_kind_of(String)
        expect(actual).to eq(expected)
      end
    end
  end

  describe "Export to Markdown with missing custom markup file test cases" do
    # The following tests export Markdown to the default markup of org-ruby
    # since the YAML file only contains the incorrect keys
    data_directory = File.join(File.dirname(__FILE__), "markdown_with_custom_markup_examples")
    org_files = File.expand_path(File.join(data_directory, "*.org" ))
    files = Dir.glob(org_files)
    files.each do |file|
      basename = File.basename(file, ".org")
      default_html_name = File.join(data_directory, basename + "_default.md")
      default_html_name = File.expand_path(default_html_name)
      custom_markup_file = File.join(data_directory, "this_file_does_not_exists.yml")
      custom_markup_file = File.expand_path(custom_markup_file)

      it "should convert #{basename}.org to Markdown with the default markup" do
        expected = IO.read(default_html_name)
        expect(expected).to be_kind_of(String)
        parser = Orgmode::Parser.new(IO.read(file), { :allow_include_files => true, :markup_file => custom_markup_file })
        actual = parser.to_markdown
        expect(actual).to be_kind_of(String)
        expect(actual).to eq(expected)
      end
    end
  end

  describe "Export to Markdown with custom markup test cases" do
    data_directory = File.join(File.dirname(__FILE__), "markdown_with_custom_markup_examples")
    org_files = File.expand_path(File.join(data_directory, "*.org" ))
    files = Dir.glob(org_files)
    files.each do |file|
      basename = File.basename(file, ".org")
      markdown_name = File.join(data_directory, basename + ".md")
      markdown_name = File.expand_path(markdown_name)
      custom_markup_file = File.join(data_directory, "custom_markup_for_markdown.yml")
      custom_markup_file = File.expand_path(custom_markup_file)

      it "should convert #{basename}.org to Markdown with custom markup" do
        expected = IO.read(markdown_name)
        expect(expected).to be_kind_of(String)
        parser = Orgmode::Parser.new(IO.read(file), {:allow_include_files => false, :markup_file => custom_markup_file })
        actual = parser.to_markdown
        expect(actual).to be_kind_of(String)
        expect(actual).to eq(expected)
      end
    end
  end

  describe "Export to HTML with incorrect custom markup test cases" do
    # The following tests export HTML to the default markup of org-ruby
    # since the YAML file only contains the incorrect keys
    data_directory = File.join(File.dirname(__FILE__), "html_with_custom_markup_examples")
    org_files = File.expand_path(File.join(data_directory, "*.org" ))
    files = Dir.glob(org_files)
    files.each do |file|
      basename = File.basename(file, ".org")
      default_html_name = File.join(data_directory, basename + "_default.html")
      default_html_name = File.expand_path(default_html_name)
      custom_markup_file = File.join(data_directory, "incorrect_markup_for_html.yml")
      custom_markup_file = File.expand_path(custom_markup_file)

      it "should convert #{basename}.org to HTML with the default markup" do
        expected = IO.read(default_html_name)
        expect(expected).to be_kind_of(String)
        parser = Orgmode::Parser.new(IO.read(file), { :allow_include_files => true, :markup_file => custom_markup_file })
        actual = parser.to_html
        expect(actual).to be_kind_of(String)
        expect(actual).to eq(expected)
      end
    end
  end

  describe "Export to HTML with missing custom markup file test cases" do
    # The following tests export HTML to the default markup of org-ruby
    # since the YAML file is missing.
    data_directory = File.join(File.dirname(__FILE__), "html_with_custom_markup_examples")
    org_files = File.expand_path(File.join(data_directory, "*.org" ))
    files = Dir.glob(org_files)
    files.each do |file|
      basename = File.basename(file, ".org")
      default_html_name = File.join(data_directory, basename + "_default.html")
      default_html_name = File.expand_path(default_html_name)
      custom_markup_file = File.join(data_directory, "this_file_does_not_exists.yml")
      custom_markup_file = File.expand_path(custom_markup_file)

      it "should convert #{basename}.org to HTML with the default markup" do
        expected = IO.read(default_html_name)
        expect(expected).to be_kind_of(String)
        parser = Orgmode::Parser.new(IO.read(file), { :allow_include_files => true, :markup_file => custom_markup_file })
        actual = parser.to_html
        expect(actual).to be_kind_of(String)
        expect(actual).to eq(expected)
      end
    end
  end

  describe "Export to HTML with custom markup test cases" do
    data_directory = File.join(File.dirname(__FILE__), "html_with_custom_markup_examples")
    org_files = File.expand_path(File.join(data_directory, "*.org" ))
    files = Dir.glob(org_files)
    files.each do |file|
      basename = File.basename(file, ".org")
      custom_html_name = File.join(data_directory, basename + ".html")
      custom_html_name = File.expand_path(custom_html_name)
      custom_markup_file = File.join(data_directory, "custom_markup_for_html.yml")
      custom_markup_file = File.expand_path(custom_markup_file)

      it "should convert #{basename}.org to HTML with custom markup" do
        expected = IO.read(custom_html_name)
        expect(expected).to be_kind_of(String)
        parser = Orgmode::Parser.new(IO.read(file), { :allow_include_files => true, :markup_file => custom_markup_file })
        actual = parser.to_html
        expect(actual).to be_kind_of(String)
        expect(actual).to eq(expected)
      end
    end
  end
end