Skip to content

Commit

Permalink
Merge pull request #22 from apiad/develop
Browse files Browse the repository at this point in the history
Add pygments
  • Loading branch information
apiad authored Dec 14, 2019
2 parents 3c9ed22 + 1942ae5 commit 9bc0b10
Show file tree
Hide file tree
Showing 13 changed files with 126 additions and 225 deletions.
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ markdown = "*"
fire = "*"
sanic = "*"
jinja2 = "*"
pygments = "*"

[requires]
python_version = "3.6"
127 changes: 67 additions & 60 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,10 @@ Staying away from `eval` and `exec` should keep you safe in most scenarios, but

## History

### v0.5.1

* Added `pygments` for code highlighting, removing `highlight.js` and fixing the error with static rendering.

### v0.5.0

* Added command `auditorium render` to generate a static HTML that can be displayed without `auditorium`.
Expand Down
1 change: 1 addition & 0 deletions auditorium/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def load(path, instance_name='show'):

return show


class Auditorium:
@staticmethod
def run(path, host='localhost', port=6789, debug=False, instance_name='show', launch=True):
Expand Down
18 changes: 18 additions & 0 deletions auditorium/demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,24 @@ def pyplot_code():
""")


@show.slide
def static_html():
"""
## Going full static
If you only need `auditorium` for the initial rendering of the HTML,
and have no animations or interactive code, you can use:
"""

show.code('auditorium render [file] > [output.html]', 'bash')

show.markdown("""
This will render the slideshow into a static HTML with all CSS and
JavaScript embedded, that you can take away and reproduce in any browser,
or host on a static file server (like Github pages).
""")


@show.slide
def themes():
"""
Expand Down
30 changes: 26 additions & 4 deletions auditorium/show.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
from sanic import Sanic
from sanic.response import html, json

from pygments import highlight
from pygments.lexers import get_lexer_by_name
from pygments.formatters.html import HtmlFormatter
from pygments.styles import get_style_by_name

from .components import ShowMode, Animation, Column, Vertical, Fragment, Block
from .utils import fix_indent
Expand All @@ -25,7 +29,7 @@ def path(p):


class Show:
def __init__(self, title="", theme='white'):
def __init__(self, title="", theme='white', code_style='monokai'):
self.slides = {}
self.slide_ids = []
self.vertical_slides = {}
Expand All @@ -43,6 +47,7 @@ def __init__(self, title="", theme='white'):
self._unique_id = 0
self._global_values = {}
self._mode = ShowMode.Edit
self._formatter = HtmlFormatter(style=get_style_by_name(code_style))

with open(path('templates/content.html')) as fp:
self._template_content = Template(fp.read())
Expand Down Expand Up @@ -175,8 +180,11 @@ def code(self, text, language='python'):
_, id_markup = self._get_unique_id("code")
content = fix_indent(text)

lexer = get_lexer_by_name(language)
code = highlight(content, lexer, self._formatter)

if self._mode == ShowMode.Markup:
self.current_content.append(f'<div {id_markup}><pre><code class="{language}">{content}</pre></code></div>')
self.current_content.append(f'<div {id_markup}>{code}</div>')
# else:
# self.current_update[item_id] = markdown(content)

Expand Down Expand Up @@ -250,8 +258,17 @@ def _embed(self, src):
with open(path(src)) as fp:
return fp.read()

def _code_style(self):
return self._formatter.get_style_defs('.highlight')

def render(self, theme=None):
return self._template_static.render(show=self, content=self._render_content(), embed=self._embed, theme=theme or self.theme)
return self._template_static.render(
show=self,
content=self._render_content(),
code_style=self._code_style(),
embed=self._embed,
theme=theme or self.theme
)

## Routes

Expand All @@ -268,4 +285,9 @@ async def _update(self, request):

async def _index(self, request):
theme = request.args.get("theme", self.theme)
return html(self._template_dynamic.render(show=self, content=self._content, theme=theme))
return html(self._template_dynamic.render(
show=self,
content=self._content,
code_style=self._code_style(),
theme=theme
))
4 changes: 4 additions & 0 deletions auditorium/static/css/auditorium.css
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
text-align: center;
}

.reveal section pre {
width: 98%;
}

.reveal section input.text:focus {
border: 0px;
border-bottom: 1px solid navy;
Expand Down
71 changes: 0 additions & 71 deletions auditorium/static/lib/css/monokai.css

This file was deleted.

Loading

0 comments on commit 9bc0b10

Please sign in to comment.