Skip to content

Commit

Permalink
Add static HTML output option from slack2html (#177)
Browse files Browse the repository at this point in the history
* Adds CustomFreezer class and methods to main for html only option.

Signed-off-by: Michael Robinson <[email protected]>

* Moves CustomFreezer class to new script freezer in slackviewer.

Signed-off-by: Michael Robinson <[email protected]>

* Modifies default output dir.

Signed-off-by: Michael Robinson <[email protected]>

* Updates readme with new cmd options  and adds help msg.

Signed-off-by: Michael Robinson <[email protected]>

* Add slack2html dep to requirements.

Signed-off-by: merobi-hub <[email protected]>

---------

Signed-off-by: Michael Robinson <[email protected]>
Signed-off-by: merobi-hub <[email protected]>
  • Loading branch information
merobi-hub authored Dec 16, 2023
1 parent f5fdfa2 commit af9e8f1
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 9 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ Options:
extract, but will not start the server, and
immediately quit.
--debug
--html-only If you want static HTML only.
-o, --output-dir PATH Output directory for static HTML. [default: `html_output`]
--help Show this message and exit.
```

Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ Werkzeug<3.0.0
Flask<3.0.0
markdown2
emoji>=2.0.0,<3.0
frozen-flask
10 changes: 10 additions & 0 deletions slackviewer/freezer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from flask_frozen import Freezer

class CustomFreezer(Freezer):

cf_output_dir = None

@property
def root(self):
return u"{}".format(self.cf_output_dir)

54 changes: 45 additions & 9 deletions slackviewer/main.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import webbrowser
import os

import click
import flask

from slackviewer.app import app
from slackviewer.archive import extract_archive
from slackviewer.reader import Reader
from slackviewer.freezer import CustomFreezer
from slackviewer.utils.click import envvar, flag_ennvar



def configure_app(app, archive, channels, no_sidebar, no_external_references, debug):
app.debug = debug
app.no_sidebar = no_sidebar
Expand All @@ -28,7 +29,6 @@ def configure_app(app, archive, channels, no_sidebar, no_external_references, de
top.mpims = reader.compile_mpim_messages()
top.mpim_users = reader.compile_mpim_users()


@click.command()
@click.option('-p', '--port', default=envvar('SEV_PORT', '5000'),
type=click.INT, help="Host port to serve your content on")
Expand All @@ -51,19 +51,55 @@ def configure_app(app, archive, channels, no_sidebar, no_external_references, de
default=flag_ennvar("SEV_NO_EXTERNAL_REFERENCES"),
help="Removes all references to external css/js/images.")
@click.option('--test', is_flag=True, default=flag_ennvar("SEV_TEST"),
help="Runs in 'test' mode, i.e., this will do an archive extract, but will not start the server,"
" and immediately quit.")
help="Runs in 'test' mode, i.e., this will do an archive extract, "
"but will not start the server, and immediately quit.")
@click.option('--debug', is_flag=True, default=flag_ennvar("FLASK_DEBUG"))
def main(port, archive, ip, no_browser, channels, no_sidebar, no_external_references, test, debug):
@click.option("-o", "--output-dir", default="html_output", type=click.Path(),
help="Output directory for static HTML files.")
@click.option("--html-only", is_flag=True, default=False,
help="If you want static HTML only, set this.")

def main(
port,
archive,
ip,
no_browser,
channels,
no_sidebar,
no_external_references,
test,
debug,
output_dir,
html_only
):
if not archive:
raise ValueError("Empty path provided for archive")

configure_app(app, archive, channels, no_sidebar, no_external_references, debug)

if not no_browser and not test:
webbrowser.open("http://{}:{}".format(ip, port))
if html_only:

# We need relative URLs, otherwise channel refs do not work
app.config["FREEZER_RELATIVE_URLS"] = True

# Custom subclass of Freezer allows overwriting the output directory
freezer = CustomFreezer(app)
freezer.cf_output_dir = output_dir

if not test:
# This tells freezer about the channel URLs
@freezer.register_generator
def channel_name():
for channel in flask._app_ctx_stack.channels:
yield {"name": channel}

freezer.freeze()

if not no_browser:
webbrowser.open("file:///{}/index.html"
.format(os.path.abspath(output_dir)))

elif not no_browser and not test:
webbrowser.open("http://{}:{}".format(ip, port))
app.run(
host=ip,
port=port
Expand Down

0 comments on commit af9e8f1

Please sign in to comment.