Skip to content

Instantly share code, notes, and snippets.

@noqqe
Last active April 17, 2020 13:27
Show Gist options
  • Save noqqe/534cb1b63c821b66f4df555180a06fc7 to your computer and use it in GitHub Desktop.
Save noqqe/534cb1b63c821b66f4df555180a06fc7 to your computer and use it in GitHub Desktop.
Helped me to identify 1000 markdown codeblocks in my hugo blog. Syntax Highlighter was quite happy after appliying this!
#!/usr/bin/env python3
import sys
import re
import signal
f = sys.argv[1]
def signal_handler(sig, frame):
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
def print_codeblock(data):
for codeline in data:
print(codeline, end = '')
if re.findall('^```\s?', codeline):
break
def what_code():
print("What language is this:")
print("p - python")
print("f - fish")
print("b - bash")
print("g - go")
print("m - markdown")
print("r - ruby")
print("h - html")
print("pu - puppet")
print("y - yaml")
print("c - custom")
try:
r = input("Choice: ")
except KeyboardInterrupt:
sys.exit(1)
if r == "p":
res = " python"
elif r == "f":
res = " fish"
elif r == "b":
res = " bash"
elif r == "g":
res = " go"
elif r == "r":
res = " ruby"
elif r == "pu":
res = " puppet"
elif r == "h":
res = " html"
elif r == "m":
res = " markdown"
elif r == "y":
res = " yaml"
elif r == "c":
res = " " + input("Enter Custom: ")
else:
res = ""
return "```" + str(res) + "\n"
# with is like your try .. finally block in this case
with open(f, 'r') as file:
# read a list of lines into data
data = file.readlines()
# loop over file lines
no=0
for pos, line in enumerate(data):
if re.findall('^```', line):
# count ``` occurences
no = no + 1
# ignore closing ``` if its the second
if no % 2 == 0:
continue
# ignore already typed codeblocks
if re.findall('^```\s?\S', line):
continue
print("\nFound Codeblock:")
print('\033[92m')
print(line, end = '')
print_codeblock(data[pos+1:])
print('\033[0m')
res = what_code()
data[pos] = res
# and write everything back
with open(f, 'w') as file:
file.writelines(data)
Found Codeblock:
```
for f in *-Ride.gpx
set -l points (math (grep -c "trkpt" $f) / 30)
gpsbabel -rt -i gpx -f $f -x simplify,count={$points} -o gpx -F SIMPLIFIED-$f
end
```
What language is this:
p - python
f - fish
b - bash
g - go
m - markdown
r - ruby
h - html
pu - puppet
y - yaml
c - custom
Choice: f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment