Edit the Sphinx Alabaster jinja template - python-sphinx

As an educational exercise, I would like to build upon the default Spinx theme Alabaster and add additional features by making changes in the jinja template.
Thus, I set up a virtual environment, installed Sphinx, copied the Alabaster folder to my working folder, _themes/Alabaster2, updated conf.py, and compiled an HTML project. This approach resulted in several missing dependencies. For example, not even the "basic" theme to be inherited could be found! None of the html_theme_options set in the conf.py file worked.
How can I overcome these shortcomings, i.e. have a working copy of Alabaster in my working folder and make changes to the jinja template?
If anyone with experience of developing themes for the Sphinx documentation system could shed some light on how to setup a environment where it is possible to tweak the templates used by Alabaster it would be most appreciated!
Additional details on my (failed) approach
Working on Windows 10, the project is set up by executing the following commands in the terminal
python -m venv .venv
.venv\scripts\activate
python -m pip install sphinx
sphinx-quickstart site #I opted for different source and build folders
cd site
mkdir _themes
xcopy ..\.venv\Lib\site-packages\alabaster _themes\alabaster2\ /E
I updated the HTML output section in source\conf.py
# -- Options for HTML output -------------------------------------------------
# The theme to use for HTML and HTML Help pages. See the documentation for
# a list of builtin themes.
#
html_theme_path = ["_themes"]
html_theme = 'alabaster2'
html_theme_options = {
'logo': 'logo.png',
'description':'Welcome to MySite',
'description_font_style':'Monospace',
'fixed_sidebar':True,
'logo_name':False,
'logo_text_align':None,
'page_width':'90%',
'sidebar_width':'280px',
# 'analytics_id':'UA-#######-##',
#'donate_url':'www.paypal.com',
# 'extra_nav_links':True,
'show_related':False,
'show_relbars':True,
'sidebar_collapse':True,
'show_powered_by':True,
}
And then in the terminal
make html
The generated build\html\index.html file does not work as expected, for example, none of the options set in the dictionary html_theme_options located in conf.py are taken in consideration.
Software versions
(.venv) >python --version
Python 3.8.8
(.venv) >sphinx-build --version
sphinx-build 4.2.0

After reading the documentation (!) I concluded that I'm going about this the wrong way, i.e. "to build upon the default Spinx theme Alabaster" you don't need to edit the Jinja templates!
For example, say you want to add the following functionalities to the default Sphinx Alabaster theme:
CSS to center align images
javascript for cookie consent
Import Bootstrap CSS
Include a logo image
In this post I will answer and comment on my own question, and show how the result illustrate below can be fairly easy achieved, with minimum coding.
Default installation on Windows 10
python -m venv .venv
.venv\scripts\activate
python -m pip install sphinx
sphinx-quickstart site #I opted for different source and build folders
cd site
Update source/conf.py
# -- Options for HTML output -------------------------------------------------
# The theme to use for HTML and HTML Help pages. See the documentation for
# a list of builtin themes.
#
html_theme = 'alabaster'
html_title = 'SOLUTION'
# Custom css files
html_css_files = [
'css/custom.css',
]
html_theme_options ={
'logo':'img/logo.png',
'description':'Washing machines works better with software',
'description_font_style':'Monospace',
'fixed_sidebar':True,
'logo_name':False,
'logo_text_align':None,
'page_width':'75%',
'sidebar_width':'250px',
# 'analytics_id':'UA-#######-##',
#'donate_url':'www.paypal.com',
# 'extra_nav_links':True,
'show_related':False,
'show_relbars':True,
'sidebar_collapse':True,
'show_powered_by':True,
}
Add custom css and html
Add source/_static/css/custom.css hosting the .centeralign class.
.centeralign {
display: block;
margin-left: auto;
margin-right: auto;
width: 50%;
margin-top: 40px;
margin-bottom: 30px;
}
Add a logo image to source/_static/img/logo.png
Add source\_templates\layout.html with the content listed below
{% extends "!layout.html" %}
{% block extrahead %}
{{ super() }}
<!-- Code for cookie consent -->
<script></script>
{% endblock %}
{% block linktags %}
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
{% endblock %}
source/index.rst
Add the following code to index.rst
.. DUST documentation master file, created by
sphinx-quickstart on Tue Oct 26 08:47:02 2021.
You can adapt this file completely to your liking, but it should at least
contain the root `toctree` directive.
Welcome to DUSTS's documentation!
=================================
Insert an image and style with Bootstrap classes.
.. code-block:: reStructuredText
.. image:: _static/img/abstract-g90ed47336_1280.jpg
:alt: DUST washing cloud
:class: img-fluid, rounded, mx-auto, d-block, shadow-sm, my-3
.. image:: _static/img/abstract-g90ed47336_1280.jpg
:alt: DUST washing cloud
:class: img-fluid, rounded, mx-auto, d-block, shadow-sm, my-3
Image by `Tomislav Jakupec <https://pixabay.com/users/tommyvideo-3092371/?utm_source=link-attribution&utm_medium=referral&utm_campaign=image&utm_content=5719221>`_ from `Pixabay <https://pixabay.com/?utm_source=link-attribution&utm_medium=referral&utm_campaign=image&utm_content=5719221>`_.
.. toctree::
:maxdepth: 2
:caption: Contents:
Indices and tables
==================
* :ref:`genindex`
* :ref:`modindex`
* :ref:`search`
The image used in the example
Image by Tomislav Jakupec from Pixabay
Make html
In the terminal execute make html and open the file build/html/index.html

Related

module files not downloading for themes

I want to change my site using Hugo. I get stuck with modules. As per documentation (e.g. this theme) I just add theme = "github.com/nodejh/hugo-theme-mini" to the config file. It fails like this:
WARN ... found no layout file for "HTML" for kind "term": You should ...
if I perform hugo mod vendor the directory structure of the theme seems created in the _vendor dir, but there is not a single file inside. which explains the "not found" error in my eyes.
nothing (!) is rendered.
I also have this effect with a couple of other themes (I thinknoteworthy being one of them).
Set up a clean project
hugo new site testModules
cd testModules
and enable Hugo modules
hugo mod init randomName`
Paste the following lines in config.toml:
baseURL = "http://example.org/"
title = "Hugo Modules Test"
[module]
[[module.imports]]
path = "github.com/nodejh/hugo-theme-mini"
Start Hugo
hugo serve -D
Done!
The content of github.com/nodejh/hugo-theme-mini" will be downloaded and used as theme.
See the theme's documentation to add content
Optional:
hugo mod vendor
will make the content of the remote repo available in the _vendor folder.
(Surprisingly the content of exampleSite was not downloaded during the quick test I made)

Folder _data in theme directory does not get the values

I'm making a Jekyll theme using the command jekyll new-theme mytheme. I want to add translations to this theme, so I created the file _data/translations.yml into the root directory. This is its content:
---
en:
post: "Post"
es:
post: "Artículo"
I get the information with this expressions:
{% assign t = site.data.translations %}
{{ t[site.lang]['post'] }}
This setup only works if I move the _data/translations.yml file to my Jekyll website directory, created through the command jekyll new mysite.
Are the data files out of themes territory? Should I place this yml file in another directory? If it's not possible to use it in the themes territory, I would like to set a default value: how can I achieve this?
Thanks in advance.
As of Jekyll 4.0, yes, data files inside a theme-gem are not read.
You'll need to use a plugin named jekyll-data (authored by me).
If you plan to publish the theme for other users, I recommend adding the plugin as a runtime dependency in the theme's gemspec. Otherwise, simply adding the plugin to the :jekyll_plugins group in the site's Gemfile will suffice.

How to use correct lang parameter?

I have a fresh Mediawiki installed into fresh UBUNTU 18 LTS... The ''SyntaxHighlight'' extension not works for Unix shell, lang="sh", lang="shell", lang="bash", ... no one is working. It is not at #Supported_languages, and there are no clues about how to install "Other markup".
At mediawiki.org/list there are no clues.
So, how to solve the problem? It is a config, env or syntax problem?
NOTES AND TESTS
Notes.
It is a corporative Wiki, no way to offer public URL... But it is a fresh, standard and controlled installation, all reproductive and standard.
The Wiki was configured with skin "Vector" and language "Brazilian Portuguese".
Tests.
Usage tests of mediawiki.org/Extension:SyntaxHighlight, the Python example.
1.1. With tag <syntaxhighlight>. Result: no highlight, same as <pre>.
1.2. With tag <source>. Result: no highlight, same as <pre>.
PHP example, fragment from wikipedia.org/PHP Syntax.
2.1. With tag <syntaxhighlight>. Result: no highlight, same as <pre>.
2.2. With tag <source>. Result: no highlight, same as <pre>.
The code fragments used in the tests:
def quickSort(arr):
less = []
pivotList = []
more = []
if len(arr) <= 1:
return arr
else:
pass
<title>PHP "Hello, World!" program</title>
<?php echo '<p>Hello World</p>'; ?>
MediaWiki Syntax highlighter used Pygments library, you should first download and install the extension:
Requirements
This version of the extension has been tested with Pygments 1.6, 2.0.2 and
MediaWiki 1.25 as of 2015-06-19. To get releases of this extension compatible
with earlier versions of MediaWiki, visit:
https://www.mediawiki.org/wiki/Special:ExtensionDistributor/SyntaxHighlight_GeSHi
Download
https://github.com/wikimedia/mediawiki-extensions-SyntaxHighlight_GeSHi/archive/master.tar.gz
Installation
Add this line to your LocalSettings.php:
wfLoadExtension( 'SyntaxHighlight_GeSHi' );
By default, this extension will use a bundled copy of Pygments 2.0.2. If you
would like to use a different copy of the library, you can set
$wgPygmentizePath to point to the path to the 'pygmentize' binary.
Usage
On the wiki page, you can now use "source" elements:
<source lang="php">
<?php
v = "string"; // sample initialization
?>
html text
<?php
echo v; // end of php code
?>
</source>
Parameters
For details information of these parameters, see the documentation of Pygments'
HtmlFormatter at http://pygments.org/docs/formatters/#HtmlFormatter.
lang; Defines the language.
line; Corresponds to linenos="inline" option.
start; Corresponds to linenostart opion.
enclose; If set to "none", corresponds to the nowrap=1 option.
inline; Corresponds to the nowrap=1 option.
highlight; Corresponds to hl_lines option (comma separated).
Note
Pygments is generous about creating HTML elements: highlighting large blocks of
code can easily generate enough of them to crash a browser. As a guard, syntax
highlighting is turned off for code fragments larger than 100 kB.

Readthedocs does not display docstring documentation

I have followed the getting started guide on Readthedocs and have used Sphinx using autodoc to create the documentation for my Python package on https://github.com/akdiem/bloodflow. (documentation relevant .rst files are in the docs folder)
The readthedoc build passed and is found on https://bloodflow.readthedocs.io/en/latest/
Readthedocs does not show any of the docstring documentation that is part of my code, but to me everything looks like it should. Why does it not?
Autodoc is a Sphinx extension that looks at its automodule directive references in .rst files during build time, imports and identifies Python code and then converts their docstrings into html.
Since your module is not installed into the environment with a setup.py,
It needs to import your module manually so you need to give sphinx context in conf.py:
import os
import sys
#Location of Sphinx files
sys.path.insert(0, os.path.abspath('./../..'))
In this case, the absolute path to your top module is 2 levels above the conf.py file.
After this you can add your autodoc directive file arteryfe.rst back to index.rst toctrees and it should be showing up.
Welcome to artery.fe's documentation!
=====================================
.. toctree::
:maxdepth: 2
:caption: Contents:
arteryfe
getting_started
tutorial
theory
numerical
In the case you ever want to make an environment installation, you'll have to tick the option for ReadTheDocs to use a virtualenvironment and utilize site-packages.
Addendum:
This is another way to do it and is particularly useful if you have more than one package.
Manually creating files with Autodoc directives can be problematic in larger codebases, so we have Sphinx Apidoc - it is an extension complementing the Autodoc extension.
This means that you may run sphinx-apidoc with the preferred options and it will generate .rst files from your Docstrings with the automodule directives - which will then generate into html. However this can also be done through conf.py during build time in RTD.
For instance, this would have Sphinx generate an automodule arteryfe.rst file in /source/_autogen during build time:
import os
import sys
#Location of Sphinx files
sys.path.insert(0, os.path.abspath('./../..'))
import sphinx.apidoc
def setup(app):
sphinx.apidoc.main(['-f', #Overwrite existing files
'-T', #Create table of contents
#'-e', #Give modules their own pages
'-E', #user docstring headers
#'-M', #Modules first
'-o', #Output the files to:
'./source/_autogen/', #Output Directory
'./../arteryfe', #Main Module directory
]
)
After that just glob all the autogen output into your toctree.
Welcome to artery.fe's documentation!
=====================================
.. toctree::
:maxdepth: 2
:caption: Contents:
getting_started
tutorial
theory
numerical
.. toctree::
:maxdepth: 2
:glob:
:caption: Code:
_autogen/*
It a bit less flexible since making templates for apidoc is complicated. It is still a valid solution and useful in some cases (i.e. huge codebases).
I've scribbled an RTD compatible Example here for apidoc multipackages.

How to use SCSS filter in Symfony2 under Windows?

Actually, this is two questions:
What is the correct way to use the SCSS filter in my Symfony project in Windows (in Twig templates) ?
I mean, how do i use the scss binary in windows?
Also, Do I necessarily need to use Compass? and "HOW" do i use compass if I have installed it?
Extension : Here is some configuration I have done:
In app/config/config.yml
assetic:
debug: %kernel.debug%
use_controller: false
filters:
scss:
bin: "%kernel.root_dir%/Resources/libs/scss"
compass:
bin: "%kernel.root_dir%/Resources/libs/compass"
In my twig file:
{% stylesheets
'#PlaylyfeBaseBundle/Resources/public/css/base.scss'
'#PlaylyfeBaseBundle/Resources/public/css/another.scss'
filter='scss'
output='css/compiled/total.css'
%}
<link rel="stylesheet" href="{{ asset_url }}" />
{% endstylesheets %}
But, when i load the page, I get the following error (inside the css file)
[exception] 500 | Internal Server Error | RuntimeException
[message] The filename, directory name, or volume label syntax is incorrect.
[1] RuntimeException: The filename, directory name, or volume label syntax is incorrect.
at n/a
in C:\wamp\www\Symfony\vendor\assetic\src\Assetic\Filter\Sass\SassFilter.php line 162
at Assetic\Filter\Sass\SassFilter->filterLoad(object(Assetic\Asset\FileAsset))
in C:\wamp\www\Symfony\vendor\assetic\src\Assetic\Filter\FilterCollection.php line 62
at Assetic\Filter\FilterCollection->filterLoad(object(Assetic\Asset\FileAsset))
in C:\wamp\www\Symfony\vendor\assetic\src\Assetic\Asset\BaseAsset.php line 83
at Assetic\Asset\BaseAsset->doLoad(&#039
I can only speak for Compass since is what I use but the same issues/problems most likely relate to the SASS/SCSS filters as well.
There are many known file path issues with Compass on Windows systems:
Issue #748: File Path normalization to support Windows systems
Pull Request #554: Fixes a bug on Windows systems
Comments on commit 539f206
... and also fixes proposed to Assetic to deal with them:
Pull Request #154: Compass problem on Windows, ruby.exe not found
Pull Request #152: Until Compass issue 554 is not corrected
Issue #131: You must compile individual stylesheets from the project directory.
I've found that doing the following was necessary for everything to work together...
#1. Make sure %ruby%\bin is in your environment PATH variable:
Example:
PATH = "...;C:\Ruby\1.9.2\bin"
#2. Edit %ruby%\bin\compass.bat to use absolute paths:
#ECHO OFF
IF NOT "%~f0" == "~f0" GOTO :WinNT
#"C:\Ruby\1.9.2\bin\ruby.exe" "C:/Ruby/1.9.2/bin/compass" %1 %2 %3 %4 %5 %6 %7 %8 %9
GOTO :EOF
:WinNT
#"C:\Ruby\1.9.2\bin\ruby.exe" "%~dpn0" %*
#3. Revert 539f206 manually in compiler.rb # line ~10:
Note: This step may not be required on the latest Ruby/Compass versions. (Reference)
Path: %ruby%\lib\ruby\gems\1.9.1\gems\compass-*\lib\compass\compiler.rb
# self.from, self.to = from.gsub('./', ''), to
self.from, self.to = File.expand_path(from), to
#4. Make sure Assetic is configured properly:
Example (config.yml):
assetic:
debug: %kernel.debug%
use_controller: false
filters:
cssrewrite: ~
compass:
bin: %compass.bin%
yui_js:
jar: %kernel.root_dir%/Resources/java/yuicompressor-2.4.7.jar
yui_css:
jar: %kernel.root_dir%/Resources/java/yuicompressor-2.4.7.jar
I use %compass.bin% in my parameters file so that I can ease the transition of the codebase between Windows and *nix systems, so my parameters.yml looks like this:
# Assetic
compass.bin: C:\Ruby\1.9.2\bin\compass.bat
#5. (Optional) Upgrade Assetic and AsseticBundle:
I have Assetic and AsseticBundle tagged to the very last possible commit that works with Symfony 2.0.x in my deps file:
[assetic]
git=http://github.com/kriswallsmith/assetic.git
version=ac71449e46bed22c276da26bf54ab2f733b3801d
[AsseticBundle]
git=http://github.com/symfony/AsseticBundle.git
target=bundles/Symfony/Bundle/AsseticBundle
version=da4a46ce37557dcf3068b8493b12bdbbe47455e2
Make sure to replace %ruby% in all of the paths above with your actual path to ruby.exe, mine being C:\Ruby\1.9.2.
Steps #2 and #4 may or may not be required, but over the course of my time fighting with this issue, it is where I've ended up and my setup works (which is all I care about!).
Good luck!
Side question: Is your path to the SCSS/Compass binaries really in %kernel.root_dir%/Resources/libs?
Unfortunately the twig scss extension is broke on windows. It is a known problem. I spent some time trying to come up with a work around but to no available. I found it best to just use the scss executable with the --watch parameter to simply create the css files and store them in the Resource/public directory. That can also simplify some deployment issues as you don't need to worry about having scss on your servers.
Use of compass is not required for scss. Think of it as a library of useful bits of css. For example, if you ever get an urge to do css rounded edges, a Compass mixin will generate all of the vendor specific custom tags. Refer to the documentation for details on using it.
In my case after hours of searching and trying many solutions, this worked for me:
In 'app/config/config.yml' add:
parameters:
# Assetic
assetic.filter.compass.bin: D:/Ruby193/bin/compass
D:/Ruby193/bin/compass will depend on your Ruby path.
See screenshot:
http://s23.postimg.org/3n2oc5wh7/MY_SOLUTION_THAT_I_FOUND.jpg
My system:
Windows 7 ultimate,
Ruby 1.9.3,
Symfony 2.4.3

Resources