Skip to content

Commit

Permalink
Added sst-snippets.py script which creates source code snippets usefu…
Browse files Browse the repository at this point in the history
…l for documentation.
  • Loading branch information
jpkenny committed Mar 7, 2024
1 parent e023c0b commit 043540e
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,5 @@ EXTRA_DIST = \
VERSION.md

include tests/Makefile.inc

bin_SCRIPTS = scripts/sst-snippets.py
64 changes: 64 additions & 0 deletions scripts/sst-snippets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#! /usr/bin/env python3

import os
import sys
import json

if not os.path.exists("snippets"):
os.mkdir("snippets")

srcpath = os.path.dirname(sys.argv[1])

configFile = open(sys.argv[1],'r')
config = json.load(configFile)

for tag in config["snippets"]:
snip = config["snippets"][tag]

# python or c++?
language = "c++"
try:
if snip["language"] == "python":
language = "python"
except: pass

# a unique tag
scoped_tag = "SSTSnippet::" + tag

# read the input file
infile = open( os.path.join(srcpath, snip["file"]), 'r')
lines = infile.readlines()

# open the output file
outfile = open( "snippets/" + snip["file"].replace("/","-") + '-' + tag, 'w')

snippeting = False
for line in lines:
if line.find(scoped_tag) != -1:
if line.find("start") != -1:
snippeting = True
elif line.find("pause") != -1:
snippeting = False
elif line.find("end") != -1:
break

if line.find("highlight") != -1:
if (language == "python"):
outfile.write("#")
else:
outfile.write("//")
if line.find("highlight-start") != -1:
outfile.write("highlight-start")
elif line.find("highlight-stop") != -1:
outfile.write("highlight-stop")
else:
outfile.write("highlight-next-line\n")
elif snippeting and line.find("SSTSnippet::") == -1:
outfile.write(line)

infile.close()
outfile.close()

configFile.close()


0 comments on commit 043540e

Please sign in to comment.