Not technically a question, but pretty sure will be helpful to many. If not helpful to you, please don't upvote.
MarkDown (the Wiki engine used on this site) respects internal (between tokens on a line) spaces within a block delimited by pre tags; but blithely interprets underscores, asterisks and less-than signs as markup. Thus rendering your shell script or Python snippet into a dire soup. The script below takes your original code, and outputs a text file with all the troublesome chars substituted to evade MarkDown's bumbling "assistance"; just cat the result, copy, and paste here within pre tags.
Here's how you use the script (say we named it ready-code-for-SplunkAnswers.sh😞
$ ready-code-for-SplunkAnswers.sh your-script.py
Result in: readied--your-script.py.txt
And here's the script:
#!/bin/bash
# Vainstein K 14nov2013
die () {
echo "(exit=$?) $@" >&2 && exit 42
}
failed () {
die 'Failed to:' $@
}
[ $# -ne 1 ] && die 'USAGE: <fileToReady>'
readonly original=$1
[ -e $original ] || die "File '$original' not found."
readonly readied=readied--$original.txt
cp -p $original $readied || failed "overwrite $readied"
chmod u+w $readied || failed "make writable $readied"
# We have to distinguish #s originally there, from the #s that we'll insert as part of escape codes.
readonly fauxOctothorpe='@XXXoctothorpeXXX@'
sed -i 's/#/'$fauxOctothorpe'/g' $readied || failed 'replace: # (phase 1 of 2)'
# Same for &.
readonly fauxAmpersand='@XXXampersandXXX@'
sed -i 's/&/'$fauxAmpersand'/g' $readied || failed 'replace: & (phase 1 of 2)'
sed -i 's/`/\`/g' $readied || failed 'replace: `'
sed -i 's/_/\_/g' $readied || failed 'replace: _'
sed -i 's/\*/\*/g' $readied || failed 'replace: *'
sed -i 's/</\</g' $readied || failed 'replace: <'
sed -i 's/\\/\\/g' $readied || failed 'replace: \\'
sed -i 's/'$fauxOctothorpe'/\#/g' $readied || failed 'replace: # (phase 2 of 2)'
sed -i 's/'$fauxAmpersand'/\&/g' $readied || failed 'replace: & (phase 2 of 2)'
# MarkDown is too stupid to leave whitespace-only lines alone; so we insert a barely-visible symbol on such.
sed -i 's/^[ \t]*$/\­/' $readied || failed 'replace: whitespace-only line'
echo "Result in: $readied"
Naturally enough, I prepared this script for upload using... itself.
;-)
Updated: be sure to read discussion with Ayn, in comments below.
hi Ayn,
OK. Let's try this silly, yet valid, shell script, each line offset by 4 leading spaces:
#!/bin/bash
two=2 three=3 four=4 # and 5 is right out
printf "Should print 24: %d\n" $((two*three*four))
echo "The hour is`date | sed -r 's/^.*( [0-9]{2}):.*/\1/'`, and all is well."
echo 'To look for "pre" preceded immediately by a less-than, try this RE: <pre\>'
And now transformed with ready-code-for-SplunkAnswers.sh, and enclosed in pre tags:
#!/bin/bash
two=2 three=3 four=4 # and 5 is right out
printf "Should print 24: %d\n" $((two*three*four))
echo "The hour is`date | sed -r 's/^.*( [0-9]{2}):.*/\1/'`, and all is well."
echo 'To look for "pre" preceded immediately by a less-than, try this RE: <pre\>'
You're right.
hi Ayn,
OK. Let's try this silly, yet valid, shell script, each line offset by 4 leading spaces:
#!/bin/bash
two=2 three=3 four=4 # and 5 is right out
printf "Should print 24: %d\n" $((two*three*four))
echo "The hour is`date | sed -r 's/^.*( [0-9]{2}):.*/\1/'`, and all is well."
echo 'To look for "pre" preceded immediately by a less-than, try this RE: <pre\>'
And now transformed with ready-code-for-SplunkAnswers.sh, and enclosed in pre tags:
#!/bin/bash
two=2 three=3 four=4 # and 5 is right out
printf "Should print 24: %d\n" $((two*three*four))
echo "The hour is`date | sed -r 's/^.*( [0-9]{2}):.*/\1/'`, and all is well."
echo 'To look for "pre" preceded immediately by a less-than, try this RE: <pre\>'
You're right.
Correct. 🙂
hi Ayn,
My solution is still the only one for code snippets which are not "block-quoted": if you want a single-line code snippet to not break up your paragraph, the four-spaces approach obviously won't do.
Almost want to say 'sorry' because that's a nice script after all 🙂
What about simply prepending each line in a code block with 4 spaces? That's how code is entered and it hasn't failed me so far...
You'd sure think the code buttton in the editor here would work. Sad that it doesn't.