# frozen_string_literal: true require 'intranet/core' require 'intranet/logger' require 'intranet/abstract_responder' require 'intranet/pandoc/responder' RSpec.describe Intranet::Pandoc::Responder do it 'should inherit from Intranet::AbstractResponder' do expect(described_class.superclass).to eql(Intranet::AbstractResponder) end it 'should define its name, version and homepage' do expect { described_class.module_name }.not_to raise_error expect { described_class.module_version }.not_to raise_error expect { described_class.module_homepage }.not_to raise_error end before do logger = Intranet::Logger.new(Intranet::Logger::FATAL) @core = Intranet::Core.new(logger) testroot = File.absolute_path('testroot', __dir__) @template = File.join(testroot, 'template.html') @responder = described_class.new(logger, 'TITLE', testroot, nil) @core.register_module( @responder, ['pandoc'], File.absolute_path('../../../lib/intranet/resources', __dir__) ) end describe '#in_menu?' do it 'should return the value provided at initialization' do expect(described_class.new(nil, '', nil, nil, false).in_menu?).to be false expect(described_class.new(nil, '', nil, nil, true).in_menu?).to be true end end describe '#resources_dir' do it 'should return the absolute path of the resources directory' do expect(described_class.new(nil, '', nil, false).resources_dir).to eql( File.absolute_path('../../../lib/intranet/resources', __dir__) ) end end describe '#title' do it 'should return the title of the webpage provided by the module' do expect(@responder.title).to eql('TITLE') end end describe '#generate_page' do context 'when asked for an existing HTML page' do it 'should return a partial HTML content generated from the index.md file' do code, mime, content = @responder.generate_page('/index.html', {}) expect(code).to eql(206) expect(mime).to eql('text/html') expect(content).to eql( { content: '
' \ "

This is index.md, the main markdown page.

\n" \ "

We may use the following text attributes:

\n" \ "\n" \ "

We may produce an horizontal rule:

\n" \ "
\n" \ "

Two spaces at the end of a line
\nproduce a line break.

\n" \ "
\n" \ "

Markdown uses email-style characters for blockquoting.

\n" \ "

Multiple paragraphs need to be prepended individually.

\n" \ "
\n" \ '
', title: 'TITLE', stylesheets: ['design/style.css'], scripts: [] } ) end it 'should shift titles by one level to correctly fit in intranet page template' do code, mime, content = @responder.generate_page('/filters/test-titles-shift-levels.html', {}) expect(code).to eql(206) expect(mime).to eql('text/html') expect(content).to eql( { content: '
' \ "

Level-1 Heading

\n" \ "

Level-2 Heading

\n" \ "

Alternative Level-1 heading

\n" \ "

Alternative Level-2 heading

\n" \ "

Level-3 heading

\n" \ "

Other heading levels are not supported by pandoc markdown.

\n" \ '
', title: 'TITLE', stylesheets: ['design/style.css'], scripts: [] } ) end it 'should add the _blank attributes to external links' do code, mime, content = @responder.generate_page('/filters/test-links-open-external-in-new-tab.html', {}) expect(code).to eql(206) expect(mime).to eql('text/html') expect(content).to eql( { content: '
' \ "

We use a filter that may produce:

\n" \ "\n" \ '
', title: 'TITLE', stylesheets: ['design/style.css'], scripts: [] } ) end it 'should use the specified template, if any, to generate the partial HTML content' do @responder.instance_variable_set(:@template, @template) code, mime, content = @responder.generate_page('/index.html', {}) expect(code).to eql(206) expect(mime).to eql('text/html') expect(content).to eql( { content: '
' \ "

This page was generated using template.html.

\n" \ "\n" \ "

This is index.md, the main markdown page.

\n" \ "

We may use the following text attributes:

\n" \ "\n" \ "

We may produce an horizontal rule:

\n" \ "
\n" \ "

Two spaces at the end of a line
\nproduce a line break.

\n" \ "
\n" \ "

Markdown uses email-style characters for blockquoting.

\n" \ "

Multiple paragraphs need to be prepended individually.

\n" \ "
\n" \ "\n" \ "

End of template

\n" \ '
', title: 'TITLE', stylesheets: ['design/style.css'], scripts: [] } ) end end context 'when asked for an existing picture' do it 'should return the picture' do # JPG file code, mime, content = @responder.generate_page('/white.jpg', {}) expect(code).to eql(200) expect(mime).to eql('image/jpeg') expect(content).to eql(File.read(File.join(__dir__, 'testroot', 'white.jpg'))) # PNG file code, mime, content = @responder.generate_page('/alpha.png', {}) expect(code).to eql(200) expect(mime).to eql('image/png') expect(content).to eql(File.read(File.join(__dir__, 'testroot', 'alpha.png'))) # SVG file code, mime, content = @responder.generate_page('/decimal.svg', {}) expect(code).to eql(200) expect(mime).to eql('image/svg+xml') expect(content).to eql(File.read(File.join(__dir__, 'testroot', 'decimal.svg'))) end end context 'otherwise' do it 'should return an HTTP 404 error' do expect(@responder.generate_page('/filters/index.html', {})).to eql([404, '', '']) expect(@responder.generate_page('index.html', {})).to eql([404, '', '']) expect(@responder.generate_page('/black.jpg', {})).to eql([404, '', '']) end end end end