Search code examples
pythonpython-3.xmarkdown

Markdown syntax highlighting a Bash command that calls a Python script


Syntax highlighting in Markdown for a Bash code block does not work when the line is one that calls a python script. It does however work for a standard Bash command such as "ls -s".

python3 py_script.py
ls -l

Does anyone know why this is and what can be done to fix this?

I've tried using "console" as the code block language descriptor but that did not produce any syntax highlighting.


Solution

  • Prelude

    Since I'm demonstrating this on Stack Overflow, I'll direct you to highlight.js: the highlighter used here (Note: changes to highlight.js in the future might break all demonstrations which follow).

    It's hard to tell which syntax highlighter is being used in your case: I would assume GitHub Pages and hence Rouge.

    Explanation

    The syntax highlighter is not using the position of words to decide highlighting, but rather the patterns.

    if true ; then true ; fi
    ls
    

    Some patterns that the highlighter recognise include the shell built-ins (if, then, fi). Similarly, the highlighter recognises the names of commands (binaries) provided by the GNU's coreutils package.

    You can see ls in the hard-coded list of GNU coreutils commands in highlight.js.

    As a result of pattern-based highlighting, Unexpected behaviour such as -ls in Unix-style flags will be recognised as a command.

    ls \
     -ls -lS
    

    Solution (?)

    You cannot really fix this.

    …except by changing your highlighter to one that parses the code block (if there is one at all).

    A reason to not parse the code would be that it's simply computationally expensive and rather complicated.


    Some other recognised patterns in the shell highlighter are

    • comments
    # this is a comment
    
    • the shebang(s) as long as they are on the first line
    #!/bin/sh
    #!/bin/zsh
    
    #!/bin/zsh
    
    • function definitions
    foo(){ echo 'bar' ;}
    # not their calls
    foo
    
    • variables and parameters
    ${such} ${as:-} ${these//} ${@}
    
    • and the similarly-coloured strings.
    "this is a string"
    myvariable="it's a small world"
    

    Note that the variable assignment did not get highlighted.

    Meanwhile even common utilities not in those lists are unrecognised

    # standard *nix utilities
    tar
    vi
    ed
    # other applications
    git
    gh
    docker
    podman
    yq
    jq
    

    An aside, console highlighting is used to show commands and outputs (or errors). But it is better formatted in Rouge.

    $ whois
    A Stack Overflow User
    
    $ python myscript.py
    You called me? 🐍
    
    $ if : ; do : ; done
    $ if true; do true; done
    
    # # it can also be used to indicate
    # # commands run as root
    # whoami
    root
    
    $ # but it make comments awkward