11

Note: I could not distinguish if this is a bug due to a wrong inclusion of Prettify, Prettify itself or wrong usage by me, since it is not explicitly documented on Prettify, how to properly format Bash scripts. There is only an indirect reference on supported languages, indicating using lang-bsh. Using lang-sh does not work either.

Update: I added also the lang tags lang-bash and lang-sh, which do not work either.

I have code of two Bash outputs mixed with one other code (e.g. JavaScript).

Formatting the following way does not highlight the Bash scripts as expected:

<!-- language: lang-bsh -->
$ npm install --save tarball-output.tgz

<!-- language: lang-js -->
let someFunc = function(){ return "proper format" }

<!-- language: lang-sh -->
ERROR: (gcloud.beta.functions.deploy) OperationError: code=3,
message=Function load error: Code in file app.js can't be loaded.
Did you list all required modules in the package.json dependencies?
Detailed stack trace: Error: Cannot find module 'my_package'

<!-- language: lang-bash -->
W20170704-12:19:27.778(2)? (STDERR) MochaRunner.runServerTests: failures: 7

Returns the following formatted codes:

$ npm install --save tarball-output.tgz
let someFunc = function(){ return "proper format" }
ERROR: (gcloud.beta.functions.deploy) OperationError: code=3,
message=Function load error: Code in file app.js can't be loaded.
Did you list all required modules in the package.json dependencies?
Detailed stack trace: Error: Cannot find module 'my_package'
W20170704-12:19:27.778(2)? (STDERR) MochaRunner.runServerTests: failures: 7

As you can see, the Bash script formattings are ignored and (somehow) receive the formatting of the JavaScript syntax. Is this a bug on Stack Overflow/Prettify or am I doing it wrong?

11
  • Did you try with lang-bash? In Stack Overflow answers it works fine to me.
    – fedorqui
    Commented Jul 4, 2017 at 10:47
  • 1
    Standalone the bash formatting works but not in combination with lang-js it seems. Updated the lang tags to show different variations.
    – Jankapunkt
    Commented Jul 4, 2017 at 11:00
  • 5
    Bash scripting is ugly anyway, so it deserves no highlighting. Closed as "by design". Commented Jul 4, 2017 at 20:45
  • 5
    Especially reading languages with bad readability gets improved by syntax highlighting. So please do not close, but support it. Although funny comment with a bit of a truth.
    – Jankapunkt
    Commented Jul 4, 2017 at 21:21
  • 1
    Spelling mistake lang-bsh for the first one. Commented Jul 4, 2017 at 22:59
  • 3
    If there is any bug with prettify, Google will be slow to fix it. Already 2 months I submitted a pull request to fix Swift highlighting. So I start to feel we should fork the Google repository under some more active organization to maintain it.
    – Cœur
    Commented Jul 5, 2017 at 3:00
  • 2
    I would suggest either using lang-none or blockquote formatting for error and log files. Personally, seeing Code formatted as a class name and in as a keyword in the error message (not to mention the comments starting at can't) makes my eyes twitch... Commented Jul 5, 2017 at 13:36
  • @MikeMcCaughan: nitpick: it's a string literal, not a comment. Anyway, +1. Commented Jul 5, 2017 at 15:36
  • I elaborated this issue a bit further: the bash highlight works but is very limited (as opposed to my editor/ide/console) and the error output gets formatted well in my editor/ide/console. Maybe I should reorganize the question towards a more sophisticated syntax highlighting css?
    – Jankapunkt
    Commented Jul 5, 2017 at 15:41
  • I'm just surprised that you're trying to use syntax highlighting on the output.
    – canon
    Commented Jul 5, 2017 at 16:10
  • Yes, I will post that in an answer in a minute to calrify things.
    – Jankapunkt
    Commented Jul 5, 2017 at 16:10

2 Answers 2

1

I elaborated, that the syntax highlighting is working (also in mixed usage with other languages) but is very limited, so that I thought, that it is not supported at all.

You can see that in the following examples (all using <!-- language: lang-bash -->):

#!/usr/bin/env bash
THIS_PATH=$(pwd)
PORT=3000
echo "this path is $THIS_PATH and port is $PORT"

Here one from the command line:

$ curl -i -H "Accept: application/json" -H "Content-Type: application/json" http://hostname/resource

Here another one from the command line with output:

$ sudo apt-get update
[sudo] Password for user:
OK:1 http://archive.canonical.com/ubuntu yakkety InRelease
OK:2 http://de.archive.ubuntu.com/ubuntu yakkety InRelease

So my thoughts on that are, that

a) Bash syntax highlight works but so limited, that I did not recoignize it, when I was formatting single line commands. Here could be an improvement on the css/highlighting side because even my cheap editor plugin has a better highlighting.

b) Bash syntax highlighting is not suitable for error output or other console output (as already mentioned in the comments). Sorry on that, that was my fault when I got tricked to think, that using this formatting makes the output look as good as on my console.

c) Code that mixes bash syntax and console otput/errors cannot be used together in the same (bash-) formatting block. Mainly because the console output looks (as already mentioned in the comments) dangerously weird formatted.

d) Using lang-bsh does not work, I misread a passage of the documentation, which states, that languages with the file ending .bsh work automatically.

e) This can either be closed or I turn it into another post about improving the quality of the bash syntax highlighting.

1
  • Well, don't do e), because, as mentioned, Stack Overflow just uses Google's Prettify script, so if you have improvements, it should go to Google, not us. Commented Jul 5, 2017 at 20:25
0

To be completely clear for future readers

(because it wasn't clear to me)

I had a problem getting this working when I first tried to make syntax highlighting work, and had to dig a bit to figure out the issue, so I will show exactly what the format must be below.

The problem with the script, as posted in your question, is that you need an empty line after the language identifier, and also need to put the language identifier (i.e. <!-- language: lang-xxx -->) to be at column 0 (not indented with 4 spaces like your code needs to be).

So it would look like the following as you type it in:

<!-- language: lang-bsh -->

    $ npm install --save tarball-output.tgz

<!-- language: lang-js -->

    let someFunc = function(){ return "proper format" }

<!-- language: lang-sh -->

    ERROR: (gcloud.beta.functions.deploy) OperationError: code=3,
    message=Function load error: Code in file app.js can't be loaded.
    Did you list all required modules in the package.json dependencies?
    Detailed stack trace: Error: Cannot find module 'my_package'

<!-- language: lang-bash -->

    W20170704-12:19:27.778(2)? (STDERR) MochaRunner.runServerTests: failures: 7

And the output will be like this:

$ npm install --save tarball-output.tgz
let someFunc = function(){ return "proper format" }
ERROR: (gcloud.beta.functions.deploy) OperationError: code=3,
message=Function load error: Code in file app.js can't be loaded.
Did you list all required modules in the package.json dependencies?
Detailed stack trace: Error: Cannot find module 'my_package'
W20170704-12:19:27.778(2)? (STDERR) MochaRunner.runServerTests: failures: 7

Note: There is no highlighting for this line of bash, per your comments, due to limited things that get highlighted in bash currently by Google Code Prettify.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .