Deployment Architecture

how can I upload code to Answers without MarkDown mangling it?

V_at_Splunk
Splunk Employee
Splunk Employee

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/`/\&#96;/g' $readied || failed 'replace: `'
sed -i 's/_/\&#95;/g' $readied || failed 'replace: _'
sed -i 's/\*/\&#42;/g' $readied || failed 'replace: *'
sed -i 's/</\&lt;/g' $readied || failed 'replace: <'
sed -i 's/\\/\&#92;/g' $readied || failed 'replace: \\'
­
sed -i 's/'$fauxOctothorpe'/\&#35;/g' $readied || failed 'replace: # (phase 2 of 2)'
sed -i 's/'$fauxAmpersand'/\&#38;/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]*$/\&shy;/' $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.

1 Solution

V_at_Splunk
Splunk Employee
Splunk Employee

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.

View solution in original post

V_at_Splunk
Splunk Employee
Splunk Employee

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.

Ayn
Legend

Correct. 🙂

0 Karma

V_at_Splunk
Splunk Employee
Splunk Employee

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.

0 Karma

Ayn
Legend

Almost want to say 'sorry' because that's a nice script after all 🙂

0 Karma

Ayn
Legend

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...

0 Karma

jrodman
Splunk Employee
Splunk Employee

You'd sure think the code buttton in the editor here would work. Sad that it doesn't.

0 Karma
Get Updates on the Splunk Community!

.conf24 | Registration Open!

Hello, hello! I come bearing good news: Registration for .conf24 is now open!   conf is Splunk’s rad annual ...

ICYMI - Check out the latest releases of Splunk Edge Processor

Splunk is pleased to announce the latest enhancements to Splunk Edge Processor.  HEC Receiver authorization ...

Introducing the 2024 SplunkTrust!

Hello, Splunk Community! We are beyond thrilled to announce our newest group of SplunkTrust members!  The ...