Example #1
0
def test_noargs(fakesite):
    assert shortcodes.apply_shortcodes(
        'test({{% noargs %}})',
        fakesite.shortcode_registry)[0] == 'test(noargs  success!)'
    assert shortcodes.apply_shortcodes(
        'test({{% noargs %}}\\hello world/{{% /noargs %}})', fakesite.
        shortcode_registry)[0] == 'test(noargs \\hello world/ success!)'
Example #2
0
def test_arg_keyword(fakesite):
    assert shortcodes.apply_shortcodes(
        'test({{% arg 1a=2b %}})', fakesite.shortcode_registry)[0] == "test(arg ()/[('1a', '2b')]/)"
    assert shortcodes.apply_shortcodes(
        'test({{% arg 1a="2b 3c" 4d=5f %}})', fakesite.shortcode_registry)[0] == "test(arg ()/[('1a', '2b 3c'), ('4d', '5f')]/)"
    assert shortcodes.apply_shortcodes('test({{% arg 1a="2b 3c" 4d=5f back=slash\\ slash %}})',
                                       fakesite.shortcode_registry)[0] == "test(arg ()/[('1a', '2b 3c'), ('4d', '5f'), ('back', 'slash slash')]/)"
Example #3
0
def test_data(fakesite):
    assert (
        shortcodes.apply_shortcodes("test({{% arg 123 %}}Hello!{{% /arg %}})", fakesite.shortcode_registry)
        == "test(arg ('123',)/[]/Hello!)"
    )
    assert (
        shortcodes.apply_shortcodes(
            "test({{% arg 123 456 foo=bar %}}Hello world!{{% /arg %}})", fakesite.shortcode_registry
        )
        == "test(arg ('123', '456')/[('foo', 'bar')]/Hello world!)"
    )
    assert (
        shortcodes.apply_shortcodes(
            'test({{% arg 123 456 foo=bar baz="quotes rock." %}}Hello test suite!{{% /arg %}})',
            fakesite.shortcode_registry,
        )
        == "test(arg ('123', '456')/[('baz', 'quotes rock.'), ('foo', 'bar')]/Hello test suite!)"
    )
    assert (
        shortcodes.apply_shortcodes(
            'test({{% arg "123 foo" foobar foo=bar baz="quotes rock." %}}Hello test suite!!{{% /arg %}})',
            fakesite.shortcode_registry,
        )
        == "test(arg ('123 foo', 'foobar')/[('baz', 'quotes rock.'), ('foo', 'bar')]/Hello test suite!!)"
    )
Example #4
0
def test_arg_pos(fakesite):
    assert shortcodes.apply_shortcodes(
        'test({{% arg 1 %}})', fakesite.shortcode_registry)[0] == "test(arg ('1',)/[]/)"
    assert shortcodes.apply_shortcodes(
        'test({{% arg 1 2aa %}})', fakesite.shortcode_registry)[0] == "test(arg ('1', '2aa')/[]/)"
    assert shortcodes.apply_shortcodes(
        'test({{% arg "hello world" %}})', fakesite.shortcode_registry)[0] == "test(arg ('hello world',)/[]/)"
    assert shortcodes.apply_shortcodes(
        'test({{% arg back\\ slash arg2 %}})', fakesite.shortcode_registry)[0] == "test(arg ('back slash', 'arg2')/[]/)"
    assert shortcodes.apply_shortcodes(
        'test({{% arg "%}}" %}})', fakesite.shortcode_registry)[0] == "test(arg ('%}}',)/[]/)"
Example #5
0
def test_arg_keyword(fakesite):
    assert shortcodes.apply_shortcodes(
        'test({{% arg 1a=2b %}})',
        fakesite.shortcode_registry)[0] == "test(arg ()/[('1a', '2b')]/)"
    assert shortcodes.apply_shortcodes(
        'test({{% arg 1a="2b 3c" 4d=5f %}})', fakesite.shortcode_registry
    )[0] == "test(arg ()/[('1a', '2b 3c'), ('4d', '5f')]/)"
    assert shortcodes.apply_shortcodes(
        'test({{% arg 1a="2b 3c" 4d=5f back=slash\\ slash %}})',
        fakesite.shortcode_registry
    )[0] == "test(arg ()/[('1a', '2b 3c'), ('4d', '5f'), ('back', 'slash slash')]/)"
Example #6
0
def test_arg_pos(fakesite):
    assert shortcodes.apply_shortcodes(
        'test({{% arg 1 %}})',
        fakesite.shortcode_registry) == "test(arg ('1',)/[]/)"
    assert shortcodes.apply_shortcodes(
        'test({{% arg 1 2aa %}})',
        fakesite.shortcode_registry) == "test(arg ('1', '2aa')/[]/)"
    assert shortcodes.apply_shortcodes(
        'test({{% arg "hello world" %}})',
        fakesite.shortcode_registry) == "test(arg ('hello world',)/[]/)"
    assert shortcodes.apply_shortcodes(
        'test({{% arg back\ slash arg2 %}})',
        fakesite.shortcode_registry) == "test(arg ('back slash', 'arg2')/[]/)"
Example #7
0
 def compile_html(self, source, dest, is_two_file=True):
     """Compile source file into HTML and save as dest."""
     makedirs(os.path.dirname(dest))
     with io.open(dest, "w+", encoding="utf8") as out_file:
         output = self.compile_html_string(source, is_two_file)
         output = apply_shortcodes(output, self.site.shortcode_registry, self.site, source)
         out_file.write()
Example #8
0
 def compile_html(self, source, dest, is_two_file=True):
     """Compile source file into HTML and save as dest."""
     makedirs(os.path.dirname(dest))
     error_level = 100
     with io.open(dest, "w+", encoding="utf8") as out_file:
         with io.open(source, "r", encoding="utf8") as in_file:
             data = in_file.read()
             output, error_level, deps = self.compile_html_string(
                 data, source, is_two_file)
             output = apply_shortcodes(output, self.site.shortcode_registry,
                                       self.site, source)
             out_file.write(output)
         deps_path = dest + '.dep'
         if deps.list:
             deps.list = [p for p in deps.list
                          if p != dest]  # Don't depend on yourself (#1671)
             with io.open(deps_path, "w+", encoding="utf8") as deps_file:
                 deps_file.write('\n'.join(deps.list))
         else:
             if os.path.isfile(deps_path):
                 os.unlink(deps_path)
     if error_level < 3:
         return True
     else:
         return False
Example #9
0
def test_data(fakesite):
    assert shortcodes.apply_shortcodes(
        'test({{% arg 123 %}}Hello!{{% /arg %}})',
        fakesite.shortcode_registry)[0] == "test(arg ('123',)/[]/Hello!)"
    assert shortcodes.apply_shortcodes(
        'test({{% arg 123 456 foo=bar %}}Hello world!{{% /arg %}})',
        fakesite.shortcode_registry
    )[0] == "test(arg ('123', '456')/[('foo', 'bar')]/Hello world!)"
    assert shortcodes.apply_shortcodes(
        'test({{% arg 123 456 foo=bar baz="quotes rock." %}}Hello test suite!{{% /arg %}})',
        fakesite.shortcode_registry
    )[0] == "test(arg ('123', '456')/[('baz', 'quotes rock.'), ('foo', 'bar')]/Hello test suite!)"
    assert shortcodes.apply_shortcodes(
        'test({{% arg "123 foo" foobar foo=bar baz="quotes rock." %}}Hello test suite!!{{% /arg %}})',
        fakesite.shortcode_registry
    )[0] == "test(arg ('123 foo', 'foobar')/[('baz', 'quotes rock.'), ('foo', 'bar')]/Hello test suite!!)"
    def compile_html(self, source, dest, is_two_file=True):
        makedirs(os.path.dirname(dest))
        try:
            command = [
                'emacs', '--batch',
                '-l', join(dirname(abspath(__file__)), 'init.el'),
                '--eval', '(nikola-html-export "{0}")'.format(abspath(source))
            ]

            # Dirty walkaround for this plugin to run on Windows platform.
            if os.name == 'nt':
                command[5] = command[5].replace("\\", "\\\\")

            output = subprocess.check_output(command)
            # Clean up text we read from stdout
            output = output.strip().decode('utf8').strip('"').replace('\\"', '"')
            with io.open(dest, "w+", encoding="utf8") as out_file:
                output = apply_shortcodes(output, self.site.shortcode_registry, self.site)
                out_file.write(output)

        except OSError as e:
            import errno
            if e.errno == errno.ENOENT:
                req_missing(['emacs', 'org-mode'],
                            'use the orgmode compiler', python=False)
        except subprocess.CalledProcessError as e:
                raise Exception('Cannot compile {0} -- bad org-mode '
                                'configuration (return code {1})'.format(
                                    source, e.returncode))
Example #11
0
 def compile_html(self, source, dest, is_two_file=True):
     """Compile source file into HTML and save as dest."""
     makedirs(os.path.dirname(dest))
     with io.open(dest, "w+", encoding="utf8") as out_file:
         output = self.compile_html_string(source, is_two_file)
         output = apply_shortcodes(output, self.site.shortcode_registry,
                                   self.site, source)
         out_file.write(output)
Example #12
0
 def compile_html(self, source, dest, is_two_file=True):
     """Compile source file into HTML and save as dest."""
     makedirs(os.path.dirname(dest))
     with io.open(dest, "w+", encoding="utf8") as out_file:
         with io.open(source, "r", encoding="utf8") as in_file:
             data = in_file.read()
         if not is_two_file:
             _, data = self.split_metadata(data)
         data = apply_shortcodes(data, self.site.shortcode_registry, self.site, source)
         out_file.write(data)
     return True
Example #13
0
 def compile_html(self, source, dest, is_two_file=True):
     """Compile source file into HTML and save as dest."""
     makedirs(os.path.dirname(dest))
     with io.open(dest, "w+", encoding="utf8") as out_file:
         with io.open(source, "r", encoding="utf8") as in_file:
             data = in_file.read()
         if not is_two_file:
             _, data = self.split_metadata(data)
         data = apply_shortcodes(data, self.site.shortcode_registry,
                                 self.site, source)
         out_file.write(data)
     return True
Example #14
0
 def compile_html(self, source, dest, is_two_file=True):
     """Compile source file into HTML and save as dest."""
     makedirs(os.path.dirname(dest))
     try:
         subprocess.check_call(['pandoc', '-o', dest, source] + self.site.config['PANDOC_OPTIONS'])
         with open(dest, 'r', encoding='utf-8') as inf:
             output = apply_shortcodes(inf.read(), self.site.shortcode_registry, self.site, source)
         with open(dest, 'w', encoding='utf-8') as outf:
             outf.write(output)
     except OSError as e:
         if e.strreror == 'No such file or directory':
             req_missing(['pandoc'], 'build this site (compile with pandoc)', python=False)
Example #15
0
 def compile_html(self, source, dest, is_two_file=True):
     """Compile source file into HTML and save as dest."""
     if markdown is None:
         req_missing(["markdown"], "build this site (compile Markdown)")
     makedirs(os.path.dirname(dest))
     self.extensions += self.site.config.get("MARKDOWN_EXTENSIONS")
     with io.open(dest, "w+", encoding="utf8") as out_file:
         with io.open(source, "r", encoding="utf8") as in_file:
             data = in_file.read()
         if not is_two_file:
             _, data = self.split_metadata(data)
         output = markdown(data, self.extensions)
         output = apply_shortcodes(output, self.site.shortcode_registry, self.site, source)
         out_file.write(output)
Example #16
0
 def compile_html(self, source, dest, is_two_file=True):
     """Compile source file into HTML and save as dest."""
     if markdown is None:
         req_missing(['markdown'], 'build this site (compile Markdown)')
     makedirs(os.path.dirname(dest))
     self.extensions += self.site.config.get("MARKDOWN_EXTENSIONS")
     with io.open(dest, "w+", encoding="utf8") as out_file:
         with io.open(source, "r", encoding="utf8") as in_file:
             data = in_file.read()
         if not is_two_file:
             _, data = self.split_metadata(data)
         output = markdown(data, self.extensions)
         output = apply_shortcodes(output, self.site.shortcode_registry,
                                   self.site, source)
         out_file.write(output)
Example #17
0
 def compile_html(self, source, dest, is_two_file=True):
     """Compile source file into HTML and save as dest."""
     makedirs(os.path.dirname(dest))
     try:
         subprocess.check_call(['pandoc', '-o', dest, source] +
                               self.site.config['PANDOC_OPTIONS'])
         with open(dest, 'r', encoding='utf-8') as inf:
             output = apply_shortcodes(inf.read(),
                                       self.site.shortcode_registry,
                                       self.site, source)
         with open(dest, 'w', encoding='utf-8') as outf:
             outf.write(output)
     except OSError as e:
         if e.strreror == 'No such file or directory':
             req_missing(['pandoc'],
                         'build this site (compile with pandoc)',
                         python=False)
Example #18
0
 def compile_html(self, source, dest, is_two_file=True):
     """Compile source file into HTML and save as dest."""
     makedirs(os.path.dirname(dest))
     error_level = 100
     with io.open(dest, "w+", encoding="utf8") as out_file:
         with io.open(source, "r", encoding="utf8") as in_file:
             data = in_file.read()
             output, error_level, deps = self.compile_html_string(data, source, is_two_file)
             output = apply_shortcodes(output, self.site.shortcode_registry, self.site, source)
             out_file.write(output)
         deps_path = dest + '.dep'
         if deps.list:
             deps.list = [p for p in deps.list if p != dest]  # Don't depend on yourself (#1671)
             with io.open(deps_path, "w+", encoding="utf8") as deps_file:
                 deps_file.write('\n'.join(deps.list))
         else:
             if os.path.isfile(deps_path):
                 os.unlink(deps_path)
     if error_level < 3:
         return True
     else:
         return False
Example #19
0
 def compile_html(self, source, dest, is_two_file=True):
     """Compile source file into HTML and save as dest."""
     makedirs(os.path.dirname(dest))
     error_level = 100
     with io.open(dest, "w+", encoding="utf8") as out_file:
         with io.open(source, "r", encoding="utf8") as in_file:
             data = in_file.read()
             output, error_level, deps = self.compile_html_string(data, source, is_two_file)
             output = apply_shortcodes(output, self.site.shortcode_registry, self.site, source)
             out_file.write(output)
         try:
             post = self.site.post_per_input_file[source]
         except KeyError:
             if deps.list:
                 self.logger.error(
                     "Cannot save dependencies for post {0} due to unregistered source file name",
                     source)
         else:
             post._depfile[dest] += deps.list
     if error_level < 3:
         return True
     else:
         return False
Example #20
0
def test_errors(fakesite):
    with pytest.raises(
            shortcodes.ParsingError,
            match=
            "^Shortcode 'start' starting at .* is not terminated correctly with '%}}'!"
    ):
        shortcodes.apply_shortcodes('{{% start',
                                    fakesite.shortcode_registry,
                                    raise_exceptions=True)

    with pytest.raises(
            shortcodes.ParsingError,
            match=
            "^Syntax error in shortcode 'wrong' at .*: expecting whitespace!"):
        shortcodes.apply_shortcodes('{{% wrong ending %%}',
                                    fakesite.shortcode_registry,
                                    raise_exceptions=True)

    with pytest.raises(
            shortcodes.ParsingError,
            match=
            "^Found shortcode ending '{{% /end %}}' which isn't closing a started shortcode"
    ):
        shortcodes.apply_shortcodes('{{% start %}} {{% /end %}}',
                                    fakesite.shortcode_registry,
                                    raise_exceptions=True)

    with pytest.raises(shortcodes.ParsingError,
                       match="^Unexpected end of unquoted string"):
        shortcodes.apply_shortcodes('{{% start "asdf %}}',
                                    fakesite.shortcode_registry,
                                    raise_exceptions=True)

    with pytest.raises(shortcodes.ParsingError,
                       match="^String starting at .* must be non-empty!"):
        shortcodes.apply_shortcodes('{{% start =b %}}',
                                    fakesite.shortcode_registry,
                                    raise_exceptions=True)

    with pytest.raises(shortcodes.ParsingError,
                       match="^Unexpected end of data while escaping"):
        shortcodes.apply_shortcodes('{{% start "a\\',
                                    fakesite.shortcode_registry,
                                    raise_exceptions=True)

    with pytest.raises(shortcodes.ParsingError,
                       match="^Unexpected end of data while escaping"):
        shortcodes.apply_shortcodes('{{% start a\\',
                                    fakesite.shortcode_registry,
                                    raise_exceptions=True)

    with pytest.raises(shortcodes.ParsingError,
                       match="^Unexpected quotation mark in unquoted string"):
        shortcodes.apply_shortcodes('{{% start a"b" %}}',
                                    fakesite.shortcode_registry,
                                    raise_exceptions=True)

    with pytest.raises(
            shortcodes.ParsingError,
            match=
            "^Syntax error in shortcode 'start' at .*: expecting whitespace!"):
        shortcodes.apply_shortcodes('{{% start "a"b %}}',
                                    fakesite.shortcode_registry,
                                    raise_exceptions=True)

    with pytest.raises(
            shortcodes.ParsingError,
            match="^Syntax error: '{{%' must be followed by shortcode name"):
        shortcodes.apply_shortcodes('{{% %}}',
                                    fakesite.shortcode_registry,
                                    raise_exceptions=True)

    with pytest.raises(
            shortcodes.ParsingError,
            match="^Syntax error: '{{%' must be followed by shortcode name"):
        shortcodes.apply_shortcodes('{{%',
                                    fakesite.shortcode_registry,
                                    raise_exceptions=True)

    with pytest.raises(
            shortcodes.ParsingError,
            match="^Syntax error: '{{%' must be followed by shortcode name"):
        shortcodes.apply_shortcodes('{{% ',
                                    fakesite.shortcode_registry,
                                    raise_exceptions=True)

    with pytest.raises(
            shortcodes.ParsingError,
            match=
            "^Found shortcode ending '{{% / %}}' which isn't closing a started shortcode"
    ):
        shortcodes.apply_shortcodes('{{% / %}}',
                                    fakesite.shortcode_registry,
                                    raise_exceptions=True)

    with pytest.raises(
            shortcodes.ParsingError,
            match="^Syntax error: '{{% /' must be followed by ' %}}'"):
        shortcodes.apply_shortcodes('{{% / a %}}',
                                    fakesite.shortcode_registry,
                                    raise_exceptions=True)

    with pytest.raises(
            shortcodes.ParsingError,
            match=
            "^Shortcode '<==' starting at .* is not terminated correctly with '%}}'!"
    ):
        shortcodes.apply_shortcodes('==> {{% <==',
                                    fakesite.shortcode_registry,
                                    raise_exceptions=True)
Example #21
0
def test_errors(site, template, expected_error_pattern):
    with pytest.raises(shortcodes.ParsingError, match=expected_error_pattern):
        shortcodes.apply_shortcodes(
            template, site.shortcode_registry, raise_exceptions=True
        )
Example #22
0
def test_positional_arguments(site, template, expected_result):
    applied_shortcode = shortcodes.apply_shortcodes(template, site.shortcode_registry)[0]
    assert applied_shortcode == expected_result
Example #23
0
def test_data(site, template, expected_result):
    applied_shortcode = shortcodes.apply_shortcodes(template, site.shortcode_registry)[0]
    assert applied_shortcode == expected_result
Example #24
0
def test_noargs(fakesite):
    assert shortcodes.apply_shortcodes(
        'test({{% noargs %}})', fakesite.shortcode_registry)[0] == 'test(noargs  success!)'
    assert shortcodes.apply_shortcodes(
        'test({{% noargs %}}\\hello world/{{% /noargs %}})', fakesite.
        shortcode_registry)[0] == 'test(noargs \\hello world/ success!)'
Example #25
0
def test_errors(fakesite):
    with pytest.raises(shortcodes.ParsingError, match="^Shortcode 'start' starting at .* is not terminated correctly with '%}}'!"):
        shortcodes.apply_shortcodes('{{% start', fakesite.shortcode_registry, raise_exceptions=True)

    with pytest.raises(shortcodes.ParsingError, match="^Syntax error in shortcode 'wrong' at .*: expecting whitespace!"):
        shortcodes.apply_shortcodes('{{% wrong ending %%}', fakesite.shortcode_registry, raise_exceptions=True)

    with pytest.raises(shortcodes.ParsingError, match="^Found shortcode ending '{{% /end %}}' which isn't closing a started shortcode"):
        shortcodes.apply_shortcodes('{{% start %}} {{% /end %}}', fakesite.shortcode_registry, raise_exceptions=True)

    with pytest.raises(shortcodes.ParsingError, match="^Unexpected end of unquoted string"):
        shortcodes.apply_shortcodes('{{% start "asdf %}}', fakesite.shortcode_registry, raise_exceptions=True)

    with pytest.raises(shortcodes.ParsingError, match="^String starting at .* must be non-empty!"):
        shortcodes.apply_shortcodes('{{% start =b %}}', fakesite.shortcode_registry, raise_exceptions=True)

    with pytest.raises(shortcodes.ParsingError, match="^Unexpected end of data while escaping"):
        shortcodes.apply_shortcodes('{{% start "a\\', fakesite.shortcode_registry, raise_exceptions=True)

    with pytest.raises(shortcodes.ParsingError, match="^Unexpected end of data while escaping"):
        shortcodes.apply_shortcodes('{{% start a\\', fakesite.shortcode_registry, raise_exceptions=True)

    with pytest.raises(shortcodes.ParsingError, match="^Unexpected quotation mark in unquoted string"):
        shortcodes.apply_shortcodes('{{% start a"b" %}}', fakesite.shortcode_registry, raise_exceptions=True)

    with pytest.raises(shortcodes.ParsingError, match="^Syntax error in shortcode 'start' at .*: expecting whitespace!"):
        shortcodes.apply_shortcodes('{{% start "a"b %}}', fakesite.shortcode_registry, raise_exceptions=True)

    with pytest.raises(shortcodes.ParsingError, match="^Syntax error: '{{%' must be followed by shortcode name"):
        shortcodes.apply_shortcodes('{{% %}}', fakesite.shortcode_registry, raise_exceptions=True)

    with pytest.raises(shortcodes.ParsingError, match="^Syntax error: '{{%' must be followed by shortcode name"):
        shortcodes.apply_shortcodes('{{%', fakesite.shortcode_registry, raise_exceptions=True)

    with pytest.raises(shortcodes.ParsingError, match="^Syntax error: '{{%' must be followed by shortcode name"):
        shortcodes.apply_shortcodes('{{% ', fakesite.shortcode_registry, raise_exceptions=True)

    with pytest.raises(shortcodes.ParsingError, match="^Found shortcode ending '{{% / %}}' which isn't closing a started shortcode"):
        shortcodes.apply_shortcodes('{{% / %}}', fakesite.shortcode_registry, raise_exceptions=True)

    with pytest.raises(shortcodes.ParsingError, match="^Syntax error: '{{% /' must be followed by ' %}}'"):
        shortcodes.apply_shortcodes('{{% / a %}}', fakesite.shortcode_registry, raise_exceptions=True)

    with pytest.raises(shortcodes.ParsingError, match="^Shortcode '<==' starting at .* is not terminated correctly with '%}}'!"):
        shortcodes.apply_shortcodes('==> {{% <==', fakesite.shortcode_registry, raise_exceptions=True)
Example #26
0
def test_noargs(fakesite):
    assert shortcodes.apply_shortcodes("test({{% noargs %}})", fakesite.shortcode_registry) == "test(noargs  success!)"
    assert (
        shortcodes.apply_shortcodes("test({{% noargs %}}\\hello world/{{% /noargs %}})", fakesite.shortcode_registry)
        == "test(noargs \\hello world/ success!)"
    )