add files from prior development
parent
f5a2b665c9
commit
1779e464e8
|
@ -0,0 +1,51 @@
|
|||
<!DOCTYPE html>
|
||||
<!-- This is an example file for PyHP (Python Hypertext Preprocessor).
|
||||
PyHP is intended to work similarly to PHP replacing the at times quirky
|
||||
PHP language with Python while still allowing for embedding in
|
||||
hypertext documents.
|
||||
-->
|
||||
<?py
|
||||
# Start of a python code block:
|
||||
# The block starts after the first line break after "<?py".
|
||||
# The indent of the first line of the block is assumed to be equivalent
|
||||
# to 0 (unless specified otherwise) in a normal python file to allow for
|
||||
# nice indented code blocks without messing up the Python syntax.
|
||||
# An example of specifying the indent is provided below.
|
||||
|
||||
title = "PyHP - Python Hypertext Preprocessor"
|
||||
text1 = "An attempt at replacing PHP with Python. "
|
||||
text2 = "This is an example file that roughly describes the format."
|
||||
?>
|
||||
<!-- End of python block:
|
||||
The block ends at the last line break before "?>".
|
||||
Unless specified otherwise, the python indent of the text after a
|
||||
python block is assumed to be 0.
|
||||
An example of specifying the indent is provided below.
|
||||
-->
|
||||
<html>
|
||||
<head>
|
||||
<!-- insert variables just like in PHP -->
|
||||
<title> <?= title ?> </title>
|
||||
</head>
|
||||
<body>
|
||||
<h1> <?= title ?> </h1>
|
||||
<p> <?= text1, text2 ?> </p>
|
||||
|
||||
<!-- Here is an example of an automatically populated table: -->
|
||||
<table>
|
||||
<?py
|
||||
hello_world = "Hello World!"
|
||||
for i in range(0,len(hello_world)+1):
|
||||
4 ?>
|
||||
<!-- python indent for the following hypertext is 4 spaces -->
|
||||
<tr>
|
||||
<td> <?= i ?> </td>
|
||||
<td> <?= hello_world ?> </td>
|
||||
</tr>
|
||||
<?py 4
|
||||
# start the python block with an indent of 4 spaces
|
||||
hello_world = hello_world[:-1]
|
||||
?>
|
||||
</table>
|
||||
</body>
|
||||
</html>
|
|
@ -0,0 +1,17 @@
|
|||
#!/usr/bin/env bash
|
||||
|
||||
if [ -z "$PYHP" ]; then
|
||||
PYHP=""$(dirname "$0")"/pyhp.py"
|
||||
fi
|
||||
|
||||
PYH="$1"
|
||||
shift
|
||||
|
||||
HYPERTEXT=$("$PYHP" "$PYH" | python3 - "$@")
|
||||
|
||||
# check for headers, if present assume the pyh file is going to handle them
|
||||
if head -n1 <<< "$HYPERTEXT" | grep "[A-Za-z][A-Za-z0-9\-]*: "; then
|
||||
;
|
||||
else
|
||||
echo -ne "Content-type: text/html\\n\\n"
|
||||
fi
|
|
@ -0,0 +1,127 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
# This is a naive implementation of the Python Hypertext preprocessor.
|
||||
# For example, we just assume that we can hold the entire code in memory.
|
||||
# Also, this is just the preprocessor, the resulting code will not be
|
||||
# executed.
|
||||
|
||||
# Yes, the comments were here first and I wrote the code after writing them and then adjusted them to reality.
|
||||
|
||||
import sys
|
||||
|
||||
fd = open(sys.argv[1], "r")
|
||||
lines = fd.read().split("\n")
|
||||
fd.close()
|
||||
|
||||
# start text
|
||||
output = "print(\"\"\""
|
||||
# while not end of file
|
||||
while len(lines)>0:
|
||||
# grab a line to process
|
||||
line = lines.pop(0)
|
||||
# iterate over line
|
||||
i=0
|
||||
while i < len(line):
|
||||
# attempt to grab the next few characters
|
||||
try:
|
||||
# variable(s) or code
|
||||
if line[i:i+3] in ["<?p", "<?="]:
|
||||
# end text
|
||||
output = output + "\"\"\""
|
||||
# variable(s)
|
||||
if line[i:i+3] == "<?=":
|
||||
output = output + " + str("
|
||||
# store variable list
|
||||
# (may span more than just one line)
|
||||
variable_list = ""
|
||||
i = i+3
|
||||
# walk until we either encounter a ?> or a line break
|
||||
while True:
|
||||
# if line break, attempt to go to next line
|
||||
if len(line[i:])<2:
|
||||
variable_list = variable_list + line[i:]
|
||||
i = -1
|
||||
try:
|
||||
line = lines.pop(0)
|
||||
except IndexError:
|
||||
print("Unexpected end of file: No closing ?> found.", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
else:
|
||||
if line[i:i+2]=="?>":
|
||||
i = i+1
|
||||
break
|
||||
else:
|
||||
# add to variable list
|
||||
variable_list = variable_list + line[i]
|
||||
i = i+1
|
||||
# split at comma and add the variables
|
||||
output = output + ") + str(".join(variable_list.split(",")) + ") + "
|
||||
# code
|
||||
elif line[i:i+4] == "<?py":
|
||||
output = output + ", end=\"\")\n"
|
||||
# determine indent
|
||||
#default to 0
|
||||
indent = 0
|
||||
try:
|
||||
# any non whitespace characters left on line?
|
||||
if not line[i+4:].strip()=="":
|
||||
# attempt to use that as indent value
|
||||
indent = int(line[i+4:])
|
||||
# determining indent failed
|
||||
except ValueError:
|
||||
print("Invalid indent: \""+line[i+4:]+"\"\n" + line, file=sys.stderr)
|
||||
sys.exit(1)
|
||||
# attempt to check for end of code
|
||||
try:
|
||||
# grab code line
|
||||
line = lines.pop(0)
|
||||
# determine appropriate unindent length to get
|
||||
# "raw" code
|
||||
unindent_length = len(line)-len(line.lstrip())
|
||||
while not "?>" in line:
|
||||
# fix indent
|
||||
line = indent*" "+line[unindent_length:]
|
||||
# add code line
|
||||
output = output + line + "\n"
|
||||
# grab next code line
|
||||
line = lines.pop(0)
|
||||
# checking for end of code failed
|
||||
except IndexError:
|
||||
print("Unexpected end of file: No closing ?> found.", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
# attempt to determine indent after end of code
|
||||
indent = 0
|
||||
try:
|
||||
if not line[:line.find("?>")].strip()=="":
|
||||
indent = int(line[:line.find("?>")])
|
||||
except ValueError:
|
||||
print("Invalid indent: \""+line[:line.find("?>")]+"\"\n" + line, file=sys.stderr)
|
||||
sys.exit(1)
|
||||
# add indent after end of code
|
||||
output = output + " "*indent + "print("
|
||||
# set i accordingly to add rest of line after code
|
||||
# block end
|
||||
i = line.find("?>")+2
|
||||
# assume accidental match
|
||||
else:
|
||||
# start text
|
||||
output = output + " + \""
|
||||
# add text
|
||||
output = output + line[i]
|
||||
# end text
|
||||
output = output + "\" + "
|
||||
# start text
|
||||
output = output + "\"\"\""
|
||||
else:
|
||||
# add text
|
||||
output = output + line[i]
|
||||
# handle last characters of line
|
||||
except IndexError:
|
||||
# add text
|
||||
output = output + line[i]
|
||||
i = i+1
|
||||
output = output + "\n"
|
||||
# end text
|
||||
output = output + "\"\"\", end=\"\")\n"
|
||||
|
||||
print(output)
|
Loading…
Reference in New Issue