Skip to content

Commit

Permalink
Add export to HTML feature
Browse files Browse the repository at this point in the history
  • Loading branch information
jcmiller11 committed Aug 4, 2023
1 parent ef17921 commit 18159b6
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 44 deletions.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

The PuzzleScript Build System!

PSBS combines multiple files into one puzzlescript source file and uploads it to the web.
PSBS combines multiple files into one puzzlescript source file and can export it to an HTML file or an online game hosted on a gist!

This is an early development release and changes may be made to the project structure and config file formats in the future.

Expand All @@ -12,7 +12,8 @@ This is an early development release and changes may be made to the project stru
- Import images and spritesheets directly into your PuzzleScript game!
- Load existing PuzzleScript projects right from their gists!
- Load existing PuzzleScript projects from a source text file!
- Save PuzzleScript projects to gists!
- Export PuzzleScript projects to html files!
- Export PuzzleScript projects to gists!
- Launch your project from play.html or the PuzzleScript editor!
- Supports most PuzzleScript forks!
- Use your favorite version control for your PuzzleScript projects!
Expand All @@ -28,7 +29,7 @@ If you don't have Python and pip installed: [Download Python](https://www.python

## Connecting to GitHub

PSBS will build your projects into PuzzleScript source without interacting with GitHub at all, however uploading and running your projects requires an authorization token.
PSBS will build your projects into PuzzleScript games without interacting with GitHub at all, however uploading and running your projects from gists requires an authorization token.

By default PSBS will attempt to run `gh auth token` to recieve an authorization token from the GitHub command line tool. If you would like to use a different token or prefer not to install the GitHub command line tool you can run the following command.

Expand All @@ -44,9 +45,9 @@ Commands:

`psbs build` Builds the project in the current working directory

`psbs upload` Builds project then uploads it to gist
`psbs export` Builds project then export to game

`psbs run` Builds project, uploads it, then runs it in your web browser
`psbs run` Builds project, exports it, then runs it in your web browser

`psbs token` Check or set the GitHub auth token

Expand Down Expand Up @@ -85,7 +86,7 @@ For more information on these commands and to see available flags enter `psbs he

At the root of your project you will find a file called config.yaml containing configuration variables for your project

- gist_id: the id of the gist the upload and run commands should use to store this project in
- gist_id: the id of the gist the export and run commands can use to store this project in
- engine: the url of the fork you are using, by default https://www.puzzlescript.net/
- template: the name of your root template file, by default main.pssengine: https://auroriax.github.io/PuzzleScript/

Expand Down
4 changes: 2 additions & 2 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<img src="media/pypi.png" style="height:2em;vertical-align:middle;">
</picture>PSBS on PyPI](https://pypi.org/project/psbs/)

PSBS combines multiple files into one puzzlescript source file and uploads it to the web.
PSBS combines multiple files into one puzzlescript source file and can export it to an HTML file or an online game hosted on a gist!

This is an early development release and changes may be made to the project structure and config file formats in the future.

Expand All @@ -25,7 +25,7 @@ If you don't have Python and pip installed: [Download Python](https://www.python

## Connecting to GitHub

PSBS will build your projects into PuzzleScript source without interacting with GitHub at all, however uploading and running your projects requires an authorization token. You can check if PSBS currently has an auth token by entering `psbs token`.
PSBS will build your projects into PuzzleScript games without interacting with GitHub at all, however uploading and running your projects from gists requires an authorization token. You can check if PSBS currently has an auth token by entering `psbs token`.

By default PSBS will attempt to run `gh auth token` to recieve an authorization token from the GitHub command line tool. If you would like to use a different token or prefer not to install the GitHub command line tool you can run the following command.

Expand Down
24 changes: 10 additions & 14 deletions docs/command-line-interface.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

[`psbs build`](command-line-interface#build) Builds the project in the current working directory

[`psbs upload`](command-line-interface#upload) Builds project then uploads it to gist
[`psbs export`](command-line-interface#export) Builds project then export to game

[`psbs run`](command-line-interface#run) Builds project, uploads it, then runs it in your web browser
[`psbs run`](command-line-interface#run) Builds project, exports it, then runs it in your web browser

[`psbs token`](command-line-interface#token) Check or set the GitHub auth token

Expand Down Expand Up @@ -59,29 +59,25 @@ Builds the project in the current working directory.

This is the heart of PSBS's functionality. Takes the source files in your project's src/ directory and compiles them into a PuzzleScript game which can will be found in your project's bin/ directory.

## Upload
## Export

`psbs upload`
`psbs export`

Builds project then uploads it to gist.
Builds project then exports it to a game.

Runs psbs build and then uploads the file to the gist id found in your project's config.yaml.
Runs psbs build and then exports your project to a game.

!> Requires a [GitHub auth token](getting-started#connecting-to-github).
If a gist id found in your project's config.yaml the game will be exported there, otherwise it will be exported to a local html file.

## Run

`psbs run [--editor,-e]`

Builds project, uploads it, then runs it in your web browser.
Builds project, exports it, then runs it in your web browser.

Runs psbs build and then uploads the file to the gist id found in your project's config.yaml, then opens up a browser to PuzzleScript's play.html pointed at that gist.
Runs psbs build, exports the project to a game, then runs it in your web browser. If a gist id is found in your project's config.yaml it will be exported there and your browser will be opened to PuzzleScript's play.html pointed at that gist. If no gist id is found a local html file will be exported and opened in your web browser.

The location of the play.html used will be determined by the engine url in your config.yaml. Change this to run the game in a different fork.

Alternatively, if the -e flag is given it will open to editor.html instead.

!> Requires a [Github auth token](getting-started#connecting-to-github).
Alternatively, if running from a gist if the -e flag is given it will open to editor.html instead.

#### Options:
- --editor, -e
Expand Down
37 changes: 37 additions & 0 deletions psbs/htmlbuilder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from requests import get
from json import dumps
from os import path

from .psparser import PSParser
from .utils import write_file

def build_html(engine, source):
standalone_url = "/".join(
engine.split("/")+["standalone_inlined.txt"]
)
response = get(standalone_url)
if response.status_code != 200:
print("Error: Can't build html game")
print(f" Unable to download {standalone_url}")
print(f" Server response: {response.status_code}")
raise SystemExit(1)
standalone_html = response.content.decode('UTF-8')
parser = PSParser(source)
title = parser.prelude_options.get("title", "My Game")
homepage = parser.prelude_options.get("homepage", engine)
if "://" in homepage:
homepage_stripped_protocol = homepage.split("://",1)[1]
else:
homepage_stripped_protocol = homepage
standalone_html = standalone_html.replace("__GAMETITLE__", parser.prelude_options.get("title", "My Game"))
standalone_html = standalone_html.replace("__HOMEPAGE__", homepage)
standalone_html = standalone_html.replace("__HOMEPAGE_STRIPPED_PROTOCOL__", homepage_stripped_protocol)
standalone_html = standalone_html.replace("___BGCOLOR___", parser.prelude_options.get("background_color", "black"))
standalone_html = standalone_html.replace("___TEXTCOLOR___", parser.prelude_options.get("text_color", "lightblue"))
# for some reason some older forks have extra quotes that need to be removed
standalone_html = standalone_html.replace("\"__GAMEDAT__\"", dumps(source))
standalone_html = standalone_html.replace("__GAMEDAT__", dumps(source))

filename = path.join("bin",title+".html")
write_file(path.join("bin",title+".html"),standalone_html)
return filename
35 changes: 25 additions & 10 deletions psbs/project.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from os import path
from shutil import rmtree
from pathlib import PurePath

from .gister import Gister
from .config import Config
from .extension import Extension
from .htmlbuilder import build_html
from .gister import Gister
from .psparser import PSParser
from .template import Template
from .extension import Extension
from .utils import read_file, write_file, write_yaml, make_dir, run_in_browser


Expand Down Expand Up @@ -39,25 +41,38 @@ def build(self):
print(f"Writing file {script_path}")
write_file(script_path, template.render())

def export(self):
if not self.config["gist_id"]:
print("Writing game to html file")
return build_html(
self.config["engine"],
read_file(path.join("bin", "script.txt"))
)
else:
self.upload()
return None

def upload(self):
gist_id = self.config["gist_id"]
if not self.config["gist_id"]:
print("Error: Unable to upload without a gist_id in config file")
raise SystemExit(1)

print("Updating gist")
gist = Gister(gist_id=gist_id)
gist = Gister(gist_id=self.config["gist_id"])
gist.write(path.join("bin", "readme.txt"))
gist.write(path.join("bin", "script.txt"))

def run(self, editor=False):
def run(self, filename, editor=False):
print("Opening in browser")
url = self.config["engine"]
if editor:
url += "editor.html?hack="
if filename:
url = PurePath(path.abspath(filename)).as_uri()
else:
url += "play.html?p="
url += self.config["gist_id"]
url = self.config["engine"]
if editor:
url += "editor.html?hack="
else:
url += "play.html?p="
url += self.config["gist_id"]
run_in_browser(url)

@staticmethod
Expand Down
21 changes: 13 additions & 8 deletions psbs/psbs.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
from argparse import ArgumentParser
from argparse import ArgumentParser, SUPPRESS
from sys import argv

from .project import PSBSProject
from .token import get_token, set_token


def main():

if len(argv) > 1 and argv[1].lower() == "upload":
print("Warning: Upload command deprecated, please use export instead in the future")
argv[1] = "export"

parser = ArgumentParser(
description="PSBS: PuzzleScript Build System", add_help=False
)
subparser = parser.add_subparsers(title="Commands", dest="command")

commands_dict = {
"build": "Build project in current working directory",
"upload": "Build project then upload to gist",
"run": "Build project, upload, then run in web browser",
"export": "Build project then export to game",
"run": "Build project, export, then run in web browser",
"new": "Create a new project",
"token": "Check or set GitHub auth token",
"help": "Display help dialog",
"help": "Display help dialog"
}
commands = {}
for command, help_text in commands_dict.items():
Expand Down Expand Up @@ -75,15 +81,14 @@ def main():
if args.command == "build":
project = PSBSProject()
project.build()
elif args.command == "upload":
elif args.command == "export":
project = PSBSProject()
project.build()
project.upload()
project.export()
elif args.command == "run":
project = PSBSProject()
project.build()
project.upload()
project.run(editor=args.editor)
project.run(project.export(),editor=args.editor)
elif args.command == "new":
PSBSProject.create(
args.name,
Expand Down
4 changes: 2 additions & 2 deletions psbs/psparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ def __init__(self, source):
for line in self.sections["prelude"].splitlines():
tokens = line.split(maxsplit=1)
if len(tokens) == 1:
self.prelude_options[tokens[0]] = True
self.prelude_options[tokens[0].lower()] = True
if len(tokens) == 2:
self.prelude_options[tokens[0]] = tokens[1]
self.prelude_options[tokens[0].lower()] = tokens[1]

class ParseError(Exception):
"""Thrown when unable to parse the input PS"""
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@

setup(
name='psbs',
version='0.2.0',
version='0.2.1',
python_requires='>=3.8',
description='PuzzleScript Build System',
long_description=long_description,
long_description_content_type='text/markdown',
author='J.C. Miller',
author_email='[email protected]',
url='https://github.com/jcmiller11/PSBS',
download_url = 'https://github.com/jcmiller11/PSBS/archive/refs/tags/0.2.0.tar.gz',
download_url = 'https://github.com/jcmiller11/PSBS/archive/refs/tags/0.2.1.tar.gz',
license='MIT',
packages=find_packages(),
package_data={'': ['example.txt']},
Expand Down

0 comments on commit 18159b6

Please sign in to comment.