I'm building documentation with Sphinx and rST. The docs are internationalized and deployed to paths like this:
website.tld/en/index.html
website.tld/de/index.html
Now on each page I'd like to give a link to the same page in different languages. Current language is available as the language variable, and the list of all languages is explicitly added to the html_context and, thus, also available.
What I struggle with is the relative path to each document. Say, there's a source document at ./source/somepath/docname.rst. It builds into these:
website.tld/en/somepath/docname.html
website.tld/de/somepath/docname.html
In the Jinja2 template, how do I get the "somepath/docname.html" value?
Suddenly, I've found the solution in an answer to a related question:
{{ pagename }}
For a source document at ./source/somepath/docname.rst, the value of pagename is "somepath/docname", which is quite what I need.
Here's the documentation for this variable.
Related
I've installed a mediawiki and imported an example page from Wikipedia. But the template is not shown properly. https://wordpress-251650-782015.cloudwaysapps.com/wiki/Cheeta
Any hint on what could be the cause?
You're most likely missing one or more required templates/Lua modules this template relies on. If you want to get all the required templates/modules you can get them via https://en.wikipedia.org/wiki/Special:Export by inserting the template name and ticking the box saying Include templates, and then importing the file generated from that via http://wordpress-251650-782015.cloudwaysapps.com/wiki/Speciale:Importa. However in most cases, except if you desperately want the exact look and feel its easier to write your one template, because Wikipedia templates get enormously complex
Is it anyhow possible (with newer XPath version maybe) to get following thing working:
//a/#href[not contains("DOMAIN OF THE CURRENT PAGE")]
DOMAIN OF THE CURRENT PAGЕ should work like variable, which gets the domain - something like {HTTP_HOST}.
I want to get all external links on this way.
If the domain of the current page exists as content of the current page, then, yes, you can select it and use it in an XPath predicate. Otherwise, no, there is no standard, universal variable defined in XPath for the domain of the current page.
Any given XPath hosting language or tool may have a mechanism to provide the domain of a page. For XPath 3.0, they might leverage the standard environment variable functions, fn:environment-variable and fn:available-environment-variables.
Alternatively, you could construct the XPath dynamically within the hosting language that knows the page – see How to pass variable parameter into XPath expression?.
I have a documentation project made with MkDocs. I would like to define global variables in the configuration file (mkdocs.yml) to be used in the markdown pages (*.md).
Reading this issue, it seems it can be done by including an extra configuration in the mkdocs.yml file, for example:
extra:
version: 1.0
... and then, use that variable in page for example as follows:
---> My version: {{ config.extra.version }}
I tried that, but unfortunately it is not working in my example (no variable substution):
Am I doing something wrong?
Is is possible to make this work?
No, this is not possible at this time.
You say that you "use that variable in page". I'm assuming you mean a "Markdown" page. At this time template variables are not available in the Markdown pages. The template engine is not even run against the Markdown. The output of the Markdown parser is one of the variables passed to the template. For a more detailed explanation of how that works, see my answer to How do you include flask/jinja2 code inside a markdown file?.
Specific to MkDocs, there is an open issue (#304) discussing adding a feature to support some limited templating within the Markdown pages, but it is scheduled for post-1.0, so its not a top priority at this time.
The given answer is out of date as this can be done with plugins like macros or markdownextradata as mentioned above except you would just reference {{ version }}.
As an update, it is possible to insert the variables from the extra slot in mkocs.yml, exactly as you describe.
To make this work, you need to install the markdownextradata plugin.
What is the difference between name.html.erb vs name.erb?
In particular, are there any reasons why name.erb could be bad to use?
I understand that name.html.erb is the convention - this indicates a HTML template and ERB Engine. But I can't find information if are there any reasons not to use name.html.erb, but name.erb instead.
My new workplace asks me to use name.erb, so I want to know: might there be any problems with this?
In short, no, there won't be any problems. Erb files simply output text. In many cases the file extension is ignored by the reading app as the reading app reads/interprets the containing text and its syntax validity. As #taglia suggests, the file extensions are mostly a 'hint' for you and may also be used by the OS to select a default app to open the file with. See here for a more thorough explanation: Output Type for an ERB File
Rails convention dictates template files to include the extension of the output type and the name of the file should end with the .erb extension. As you mentioned, name.html.erb indicates an HTML template and ERB extension that allows any instance variables in your controller's index action to get passed into the template and used. Similarly, name.js.erb indicates a JavaScript template. See here under 'Conventions or Template Files': An Introduction to ERB Templating
ERB is just a templating language, it is not limited to HTML (you could have name.txt.erb, or name.js.erb). Removing html from the name is just going to make your life more difficult (assuming it works), because you won't be able to know what file you are dealing with unless you open it.
My goal is to create links from any published jekyll page back to its location on Github.
And so, I'd need access to a page's pathname when constructing this url. However, I don't see anything in the api for templates that provides this info. I looked at the source for page, but none of the file/path attributes had values, when accessed via the template.
Update: nowadays you can use {{page.path}}.
It seems like you can construct your link just fine using the liquid code: {{ page.url }}
For instance, if you want this page: http://railsdocs.org/pages/get-involved.html to link to this page: https://github.com/dogweather/railsdocs.org/blob/gh-pages/pages/get-involved.md
Seems like you could add something like:
[source](https://github.com/dogweather/railsdocs.org/blob/gh-pages/{{page.url | replace:'.html','.md'}})
to the markdown and get the link you want.
A more general solution:
Alternatively, we could write a generator that allows a page to access its file name directly. Add this to a .rb file in your _plugins directory:
module Jekyll
class PagePathGenerator < Generator
safe true
## See post.dir and post.base for directory information.
def generate(site)
site.posts.each do |post|
post.data['path'] = post.name
end
end
end
end
and then we can reliably get the post's filename as {{ page.path }}. This solution is more robust than converting from the URL, since page names can have characters that get 'sanitized' out when converting them to URLs. (Posts would also need their date information, etc added back in). Instead, this plugin gives us direct access to a post's name.
A similar strategy could allow us to get the path to the post if that data is also needed.
I'm not sure when this was added, but page.path gives the source file's path relative to the Jekyll root directory.