htdebeer / pandocomatic

Automate the use of pandoc

Home Page:https://heerdebeer.org/Software/markdown/pandocomatic/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Relavtive path for recursive usage

mogoh opened this issue · comments

Let's consider this configuration:

settings:
  recursive: true
templates:
  website:
    pandoc:
      from: markdown
      to: html
      standalone: true
      css: ./style/index.css

I have this files:

/style/index.css
/index.md
/foo/index.md

This generates:

/style/index.css
/index.html
/foo/index.html

index.html contains:

<link rel="stylesheet" href="style/index.css" />

This relative link works in the root directory but not in foo/index.html.

Is there a solution to this problem?

I can reproduce the issue.

As a workaround, you can create a custom HTML template for use in pandoc --template yourtemplate.html and add the link to the stylesheet to that template. However, when I tried this just now, somehow it does not accept relative URLs either. However, adding it as a absolute URL does work.

I will look further into these issues.

Thank you for your quick response.

A bit off-topic/background:
I am experimenting with pandocomatic as a static site generator.
My work in progress is here: https://gitlab.com/mogoh/pandocomatic-static-site-generator

This page doesn't get the correct CSS-URL: https://mogoh.gitlab.io/pandocomatic-static-site-generator/test/foo.html

I think I have found the solution: replacing css: ./style/index.css in your config by css: "/style/index.css" should work. The value of the css option is just a string; the file it refers to is not read or processed by pandoc. That string value is put into the HTML template as is. Does this make sense?

Almost right, but with a little adjustment:

I tried it and it leads to this link:

<link rel="stylesheet" href="/style/index.css">

This doesn't work in my case but if I set it to /pandocomatic-static-site-generator/style/index.css it works.

But that is not that nice, because I hard code the base path.
If someone forks my repository, it would needs to be adjusted.

Can I do something like:

$BASEPATH = '/pandocomatic-static-site-generator'
css: $BASEPATH + '/style/index.css'

Also: With absolute path's it doesn't work any more without a webserver.
One can't just open the html-file in a browser any more.

No. Although you can specify paths relative to the data directory, that only works for assets used during generation by pandocomatic.

Usually I specify the assets linked to in HTML files from the root "/" of the resulting web site. I setup my pandocomatic static site projects like so:

    /
    - config.yaml    (pandocomatic configuration file)
    - data-dir/         (data directory for pandoc templates, filters, and stuff)
      - templates/
        - my-web-template.html
    - src/                (the source files including the HTML assets like images, stylesheets and stuff)
      - assets/
        - img/
          - logo.png
        - css/
          - main.css
        - index.md
        - foo/
          - index.md
    - www/             (for the output of pandocomatic)

Then I call pandocomatic with pandocomatic -c config.yaml -d data-dir -i src -o www. After that, I copy the contents of the www to the root of my actual web server. (I use a script for this, but you get the gist).

I suppose I have implicitly assumed that pandocomatic's output would be placed at the server's root and run on a web server. Of course, this assumption does not hold in your case nor if you want to run it locally.

I look if pandocomatic can support your use case. If so, it probably end up with some way to set the "web root" like you suggest with the $BASEPATH. But I need to investigate what sort of effect this will have on pandocomatic's behavior for other use cases.

Thank you! 😄

Issue

Description

When using the pandoc option --css SOMEPATH, the output HTML file will contain a line like <link rel="stylesheet" href="SOMEPATH" />. The path SOMEPATH is interpreted by a web browser starting from the context of the location of the output HTML file. Of course, that output HTML file can be moved elsewhere, in which case SOMEPATH might no longer be reachable or even point to a different file.

Looking through pandoc's options, the --css option might be the only one with this issue. Other options with paths do not treat the path as a value to put in the output. Instead, the contents of the file are read and used while running pandoc. For example, the option --include-in-header SOMEPATH puts the contents of the file at SOMEPATH in the output file. You can do that also with the --css option by combining it with the --self-contained option.

The only other place where this issue might occur is when using pandoc's image element. However, it seems more common to specify pictures relative to the input/output files and most pictures are not used globally on a web site like a CSS file might. As a result, paths in the image element are not included in this issue.

Currently pandocomatic supports three different kind of paths: relative paths starting with ./, absolute paths starting with /, and "relative to data directory" paths that start with neither ./ nor /. This seems to work fine for most situations, but not necessarily for the --css option. There is an implicit assumption that web sites generated with pandocomatic are served by a web server from the web server's root. If this assumption is not met, using the --css option might result in the CSS files specified cannot be found. For example, when viewing the web site directly from the file system (using a file:/// URL) or moving the web site to another directory.

Suggested solution

  1. Add a fourth type of path specification that indicates a path relative from a root path. Paths starting with "$ROOT$" are treated as relative to a root path.
  2. Add a command-line option and pandocomatic option to set that root path, --root-path ROOT_PATH or -r ROOT_PATH. It defaults to the directory of the root of the output. For example, when invoking pandocomatic with --output /var/www/project/overview.html, the root path is /var/www/project.

When processing pandoc path options, if the path is relative to the root path, the path is rewritten to first go to the root path (using ../) and then follow the path specified. As a result, all references will be relative to a specific output file. Unfortunately, when using root path specification for other path options than --css or together with the --self-contained options, the user needs to take into account that the behavior is slightly counter-intuitive as you have to reason about the output file's location while working with the input file.

Questions

  • Are there more options that behave like --css?
  • Should the user be able to specifiy the root path?
    • via a command-line option?
    • via a configuration option? (Does not make much sense)
    • or always default to the root of the output? (No, only when not set via the comman-line option)

I've build the feature. I marked it as "experimental" for now. Can you test it? It is in pandocomatic version 0.2.7.0.

I have tried it but something does not work out. Maybe it is a misconfiguration.

$ pandocomatic --input src --output public --data-dir data --root-path pandocomatic-static-site-generator
9 commands to execute to convert '/home/mogoh/src/gitlab.com/mogoh/pandocomatic-static-site-generator/src' to 'public'.
(9) + convert /home/mogoh/src/gitlab.com/mogoh/pandocomatic-static-site-generator/src; enter public
(7)   - convert index.md -> index.html
An unexpected error has occurred. You can report this bug via https://github.com/htdebeer/pandocomatic/issues/new.
Traceback (most recent call last):
        20: from /usr/local/bin/pandocomatic:23:in `<main>'
        19: from /usr/local/bin/pandocomatic:23:in `load'
        18: from /var/lib/gems/2.5.0/gems/pandocomatic-0.2.7.0/bin/pandocomatic:3:in `<top (required)>'
        17: from /var/lib/gems/2.5.0/gems/pandocomatic-0.2.7.0/lib/pandocomatic/pandocomatic.rb:105:in `run'
        16: from /var/lib/gems/2.5.0/gems/pandocomatic-0.2.7.0/lib/pandocomatic/command/convert_list_command.rb:94:in `execute'
        15: from /var/lib/gems/2.5.0/gems/pandocomatic-0.2.7.0/lib/pandocomatic/command/convert_list_command.rb:94:in `each'
        14: from /var/lib/gems/2.5.0/gems/pandocomatic-0.2.7.0/lib/pandocomatic/command/convert_list_command.rb:95:in `block in execute'
        13: from /var/lib/gems/2.5.0/gems/pandocomatic-0.2.7.0/lib/pandocomatic/command/convert_file_multiple_command.rb:99:in `execute'
        12: from /var/lib/gems/2.5.0/gems/pandocomatic-0.2.7.0/lib/pandocomatic/command/convert_file_multiple_command.rb:99:in `each'
        11: from /var/lib/gems/2.5.0/gems/pandocomatic-0.2.7.0/lib/pandocomatic/command/convert_file_multiple_command.rb:100:in `block in execute'
        10: from /var/lib/gems/2.5.0/gems/pandocomatic-0.2.7.0/lib/pandocomatic/command/command.rb:129:in `execute'
         9: from /var/lib/gems/2.5.0/gems/pandocomatic-0.2.7.0/lib/pandocomatic/command/convert_file_command.rb:86:in `run'
         8: from /var/lib/gems/2.5.0/gems/pandocomatic-0.2.7.0/lib/pandocomatic/command/convert_file_command.rb:135:in `convert_file'
         7: from /var/lib/gems/2.5.0/gems/pandocomatic-0.2.7.0/lib/pandocomatic/command/convert_file_command.rb:184:in `pandoc'
         6: from /var/lib/gems/2.5.0/gems/pandocomatic-0.2.7.0/lib/pandocomatic/command/convert_file_command.rb:184:in `chdir'
         5: from /var/lib/gems/2.5.0/gems/pandocomatic-0.2.7.0/lib/pandocomatic/command/convert_file_command.rb:186:in `block in pandoc'
         4: from /var/lib/gems/2.5.0/gems/pandocomatic-0.2.7.0/lib/pandocomatic/command/convert_file_command.rb:186:in `each'
         3: from /var/lib/gems/2.5.0/gems/pandocomatic-0.2.7.0/lib/pandocomatic/command/convert_file_command.rb:192:in `block (2 levels) in pandoc'
         2: from /var/lib/gems/2.5.0/gems/pandocomatic-0.2.7.0/lib/pandocomatic/configuration.rb:559:in `update_path'
         1: from /var/lib/gems/2.5.0/gems/pandocomatic-0.2.7.0/lib/pandocomatic/configuration.rb:854:in `make_path_root_relative'
/var/lib/gems/2.5.0/gems/pandocomatic-0.2.7.0/lib/pandocomatic/configuration.rb:854:in `realpath': No such file or directory @ realpath_rec - /home/mogoh/src/gitlab.com/mogoh/pandocomatic-static-site-generator/src/public (Errno::ENOENT)

So the error say that /home/mogoh/src/gitlab.com/mogoh/pandocomatic-static-site-generator/src/public is missing.
That should not be the case, but let's create one.
Now we get this:

$ pandocomatic --input src --output public --data-dir data --root-path pandocomatic-static-site-generator
9 commands to execute to convert '/home/mogoh/src/gitlab.com/mogoh/pandocomatic-static-site-generator/src' to 'public'.
(9) + convert /home/mogoh/src/gitlab.com/mogoh/pandocomatic-static-site-generator/src; enter public
(7)   - convert index.md -> index.html
An unexpected error has occurred. You can report this bug via https://github.com/htdebeer/pandocomatic/issues/new.
Traceback (most recent call last):
        20: from /usr/local/bin/pandocomatic:23:in `<main>'
        19: from /usr/local/bin/pandocomatic:23:in `load'
        18: from /var/lib/gems/2.5.0/gems/pandocomatic-0.2.7.0/bin/pandocomatic:3:in `<top (required)>'
        17: from /var/lib/gems/2.5.0/gems/pandocomatic-0.2.7.0/lib/pandocomatic/pandocomatic.rb:105:in `run'
        16: from /var/lib/gems/2.5.0/gems/pandocomatic-0.2.7.0/lib/pandocomatic/command/convert_list_command.rb:94:in `execute'
        15: from /var/lib/gems/2.5.0/gems/pandocomatic-0.2.7.0/lib/pandocomatic/command/convert_list_command.rb:94:in `each'
        14: from /var/lib/gems/2.5.0/gems/pandocomatic-0.2.7.0/lib/pandocomatic/command/convert_list_command.rb:95:in `block in execute'
        13: from /var/lib/gems/2.5.0/gems/pandocomatic-0.2.7.0/lib/pandocomatic/command/convert_file_multiple_command.rb:99:in `execute'
        12: from /var/lib/gems/2.5.0/gems/pandocomatic-0.2.7.0/lib/pandocomatic/command/convert_file_multiple_command.rb:99:in `each'
        11: from /var/lib/gems/2.5.0/gems/pandocomatic-0.2.7.0/lib/pandocomatic/command/convert_file_multiple_command.rb:100:in `block in execute'
        10: from /var/lib/gems/2.5.0/gems/pandocomatic-0.2.7.0/lib/pandocomatic/command/command.rb:129:in `execute'
         9: from /var/lib/gems/2.5.0/gems/pandocomatic-0.2.7.0/lib/pandocomatic/command/convert_file_command.rb:86:in `run'
         8: from /var/lib/gems/2.5.0/gems/pandocomatic-0.2.7.0/lib/pandocomatic/command/convert_file_command.rb:135:in `convert_file'
         7: from /var/lib/gems/2.5.0/gems/pandocomatic-0.2.7.0/lib/pandocomatic/command/convert_file_command.rb:184:in `pandoc'
         6: from /var/lib/gems/2.5.0/gems/pandocomatic-0.2.7.0/lib/pandocomatic/command/convert_file_command.rb:184:in `chdir'
         5: from /var/lib/gems/2.5.0/gems/pandocomatic-0.2.7.0/lib/pandocomatic/command/convert_file_command.rb:186:in `block in pandoc'
         4: from /var/lib/gems/2.5.0/gems/pandocomatic-0.2.7.0/lib/pandocomatic/command/convert_file_command.rb:186:in `each'
         3: from /var/lib/gems/2.5.0/gems/pandocomatic-0.2.7.0/lib/pandocomatic/command/convert_file_command.rb:192:in `block (2 levels) in pandoc'
         2: from /var/lib/gems/2.5.0/gems/pandocomatic-0.2.7.0/lib/pandocomatic/configuration.rb:559:in `update_path'
         1: from /var/lib/gems/2.5.0/gems/pandocomatic-0.2.7.0/lib/pandocomatic/configuration.rb:854:in `make_path_root_relative'
/var/lib/gems/2.5.0/gems/pandocomatic-0.2.7.0/lib/pandocomatic/configuration.rb:854:in `realpath': No such file or directory @ realpath_rec - /home/mogoh/src/gitlab.com/mogoh/pandocomatic-static-site-generator/pandocomatic-static-site-generator (Errno::ENOENT)

So /home/mogoh/src/gitlab.com/mogoh/pandocomatic-static-site-generator/pandocomatic-static-site-generator is also missing.
OK, let's create that one, too.

Now things are getting translated, but not in the right way.

The generated output:

  <link rel="stylesheet" href="/home/mogoh/src/gitlab.com/mogoh/pandocomatic-static-site-generator/pandocomatic-static-site-generator/style/index.css" />

The desired output:

  <link rel="stylesheet" href="/pandocomatic-static-site-generator/style/index.css" />

Again, maybe I did not configure things right. 🤔

I will look into this further. At the moment I discern two cases: 1) the output path is in a (sub) directory of the root path and I construct a relative path to that root path first before combining the CSS path, or 2) it is not and I just combine root path and CSS path.

It is possible I missed a case or two, so I will investigate and add some extra test cases.

I've created a fix for the issue. The root path should work even when that root path does not exist in the context of the current working directory or the output directory. Can you try your tests again? Now with pandocomatic 0.2.7.1.