6

Consider the following function

# in mymodule.py:

def myfunc(num,mystring):
    """
    replicates a string

    :param num: a positive integer
    :param mystring: a string
    :return: string concatenated with itself num times

    :Example:
        >>> num = 3
        >>> mystring = "lol"
        >>> myfunc(num, mystring)
        "lollollol"
    """
    return num*mystring

If I use Sphinx to generate code from my docstring, it looks like this (I'm using the default theme haiku this time, without many changes in the rst or conf.py file):

enter image description here

Are there any themes -or any other workarounds- that resolve the problems that I indicated with colors?

I tried different ones, and none worked, but I couldn't get any of the points 1.-4. to work.

EDIT The idea is that I don't want to edit the HTML, which is way to cumbersome. I want to make changes only to conf.py and index.rst files in my Sphinx docs directory in order to make the mentioned changes.

mzjn
  • 48,958
  • 13
  • 128
  • 248
l7ll7
  • 1,309
  • 1
  • 10
  • 20
  • What exactly did you try? A very general answer is that the look and feel of a theme can be customized with CSS. See https://stackoverflow.com/q/57415129/407651. – mzjn Oct 01 '19 at 14:35
  • @mzjn I tried other themes like `guzzle_sphinx_theme`, but none of the ones I tried executed the changes that I wished. I also googled whether there were any option in the conf.py and index.rst files that make the changes but couldn't find much (but I'm sure there must be such chagnes). See also my edit. – l7ll7 Oct 01 '19 at 15:37
  • Did you try any CSS customization? I suspect that in order to solve all your issues, CSS alone might not be enough. But we have to start somewhere. And you have asked four questions here. It is usually better to ask one distinct question at a time. – mzjn Oct 01 '19 at 15:42
  • @mzjn The thing is though that I don't want to edit the CSS inside the Sphinx generated HTML file - which seems what you are asking me to do. Please see my edit. I want to make the changes from within Sphinx. – l7ll7 Oct 01 '19 at 15:46
  • You are not supposed to edit the generated HTML or CSS. You need to create a custom CSS file in the Sphinx project with your changes. See the question that I linked to in my first comment. – mzjn Oct 01 '19 at 15:48
  • @mzjn Ah, gotcha now! Do you have any tutorials or resources were people did that? I have (almost) never created/edited CSS files, since I have no web background, so it owuld be much easier, if I could adapt someone elses solution. – l7ll7 Oct 01 '19 at 17:31
  • Actually, are you really sure that a CSS will even solve 2? Maybe I'm too n00b, but it seems to me that CSS won't be able to solve 2, since it can't tell whether "num" is an argument of the function and should be displayed differently (Let alone, e.g. 4 which probably need to be done by Sphinx, to do semantic highlighting.) – l7ll7 Oct 01 '19 at 17:38

1 Answers1

5

CSS customization alone cannot solve this. Even though you can use ::before and after ::after selectors, I think JavaScript is better suited to deal with this.

You can add a custom Javascript file using the html_js_files option in conf.py. Your requirements 1, 2, and 3 should be possible using this.

#  ...

html_js_files = ['custom.js']

# ...

If you are using the code above in conf.py then your custom.js file should be located as follows

docs/
  _static/
    custom.css
    custom.js
  _templates/
  index.rst
  api.rst
  ...

An Example

Before adding custom.js

enter image description here

My custom.js file

window.onload = function () {
    // Change 'Parameters' to 'Arguments' 
    first_dt_elements = document.querySelectorAll('dl.field-list > dt:first-child');
    for(let i = 0; i< first_dt_elements.length; i++){
        first_dt_elements[i].innerText="Arguments";
    }
    // arguments same font and typeface etc..
    dl_methods = document.querySelectorAll('dl.function, dl.method');
    parameter_texts = []
    for(let i = 0; i< dl_methods.length; i++){
        params = [];
        param_elems=dl_methods[i].querySelectorAll('.sig-param');
        for (let j = 0; j< param_elems.length; j++){
            params.push(param_elems[j].innerText);
        }
        for( let k=0; k<params.length; k++){
            str = dl_methods[i].innerHTML;
            dl_methods[i].innerHTML= str.replace(
                RegExp(params[k], "g"), 
                '<span style="color:red;font-family:monospace;font-style:normal;">'
                + params[k] +
                '</span>'
            );
        }
    }
}

After building the html again

enter image description here

Regarding the optional requirement 4

Sphinx uses pygments for synatx highlighting. If you are not satisfied with this, you can use Highlightjs, Prismjs, Google code prettify etc.. by downloading and including them through html_js_files. But these are also syntax highlighters. Semantic highlighters that highlight variables are available for python, but I have not heard of anything that can be used with Sphinx.

Note:

  • I am using Alabaster theme. But this idea should work on most themes. However, you will have to write the custom.js file according to your theme.
  • My custom.js file is very simplistic and does not take into account all possibilities.
Bitto
  • 7,937
  • 1
  • 16
  • 38
  • dayummm, this is a long answers! +1 simply for the effort. now let me read through it to see how it works. – l7ll7 Oct 08 '19 at 14:51
  • Ok, I tested it. For some reason it doesn't seem to work, it doesn't pick up in the javascript code. I placed the `custom.*` files in the `_built` folder (BTW, if `custom.css` is empty, why do I need to even create it), added the line of `conf.py`, and `make html` compiles it without problems, but no change appears. Do you have any idea what I'm doing wrong? – l7ll7 Oct 10 '19 at 10:33
  • As a minor side-question, do you know how to disable the bold typeface of the argument names, `name_str` and `default` when they appear near "Arguments" in your picture so the argument names appear *exactly the same* everywhere? I don't know javascript and I could not find any option in your code that seems to refer to boldface. Also, do you know (in one short sentence) the `::before` / `::after` selectors that you mention work, so that I may use them in case I'll need them to customize your file? There does not seem to be a lot of good documentation about this online.) – l7ll7 Oct 10 '19 at 10:36
  • (P.S. I've awarded the bounty because it will expire otherwise, but please let me know how to get the javascript customization to work.) – l7ll7 Oct 10 '19 at 10:39
  • @mzjn This worked for me in Alabaster theme. I don't know if the OP was able to get it working finally after changing the custom.js file. It will be very difficult to do if someone is not familiar with css and Js. – Bitto Oct 13 '19 at 15:05
  • @mzjn from my previous comment: " I've awarded the bounty because it will expire otherwise" – l7ll7 Oct 13 '19 at 17:45
  • @BittoBennichan I is still not working unfortunately. I don't know any CSS, or Js, but I don't think I need to, right? I just followed your instructions 1-to-1, with the Alabaster theme (which, as I understand, is the default theme, so no changes needed to be made to `conf.py`), and it should work, right? Could you please also post your `conf.py`; maybe that is were the problem was? – l7ll7 Oct 13 '19 at 17:46
  • @mzjn whoops. accepting was a reflex, when I awarded the bounty. corrected now, until this actually gets solved. – l7ll7 Oct 13 '19 at 17:47
  • @l7ll7 No You should change the custom.js file for your theme. – Bitto Oct 13 '19 at 17:48
  • @BittoBennichan from Sphinx docs: "Alabaster is an install-time dependency of Sphinx and is selected as the default theme". Since I haven't set any theme, it should use Alabaster right? (I don't really care about any particular theme, as long as I can get this to work...) – l7ll7 Oct 13 '19 at 17:50
  • @l7ll7 Depends on the version of sphinx you are using. It is the default theme from sphinx 1.3 – Bitto Oct 13 '19 at 17:51
  • I'm running Sphinx v1.8.3. I just carefully looked at it: Indeed I am using Alabaster (even trying `pip install alabaster` confirms that it's already installed) and explicitly invoking `html_theme = 'alabaster'` in `conf.py` doesn't produce a change. So the problem must be somewhere else that it is not picking up on the CSS/Js. Is perhaps the location of `custom.js` relevant. Also notice my earlier question: `custom.css` is empty. Is that really how it's supposed to be? – l7ll7 Oct 13 '19 at 17:56
  • @I7II7: But in the question you say that you use the haiku theme. – mzjn Oct 13 '19 at 17:58
  • @mzjn True. But after Bitto mentioned Alabaster, I changed it to that. (I tried both: Commented out the whole theme line -so Alabaster should be used by default- as well as explicitly using Alabaster.) Would have been easier if Bitto would directly have used `haiki` as I mentioned it in my question, but ultimately Alabster is fine too - *if* this approach will work. – l7ll7 Oct 13 '19 at 17:59
  • @l7ll7 You can check if the custom.js files are loaded by checking the page source. Right click on the page -> view page source. – Bitto Oct 13 '19 at 18:00
  • @BittoBennichan It indeed is loaded: `` Very odd, what is happening here. Could you perhaps post your `index.rst` and `conf.py` files? I have no other idea were to look what could mess this up. – l7ll7 Oct 13 '19 at 18:07
  • @l7ll7 Lets dicuss in chat : https://chat.stackoverflow.com/rooms/200783/question-discussion-room – Bitto Oct 13 '19 at 18:10