Skip to content

Commit

Permalink
Enable custom font size (#40)
Browse files Browse the repository at this point in the history
* Update example config

* Accept font_size in output generator

* Add font-size argument to main multiparser

* Default to using integer font size

* Update changelog; reduce example config for speed

* Update version marker
  • Loading branch information
j6k4m8 committed Apr 29, 2021
1 parent 77f52d7 commit 44a7e60
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 168 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
@@ -1,8 +1,8 @@
# CHANGELOG

### **???** (Unreleased)
### **0.3.1** (April 29 2021)

> This version adds a test suite and improvements to the upload mechanism.
> This version adds a test suite and improvements to the upload and config mechanisms, as well as several more performance and feature improvements.
- Improvements
- Add test suite
Expand Down
10 changes: 2 additions & 8 deletions example-config.json
@@ -1,4 +1,5 @@
{
"font_size": 12,
"stories": [
{
"provider": "weather",
Expand All @@ -7,7 +8,7 @@
{
"provider": "twitter",
"config": {
"usernames": ["reuters", "bbcWorld", "axios", "NPR"],
"usernames": ["axios", "NPR"],
"limit_per": 8
}
},
Expand All @@ -22,13 +23,6 @@
"limit": 5
}
},
{
"provider": "rss",
"config": {
"rss_path": "https://www.statnews.com/feed/",
"limit": 2
}
},
{
"provider": "reddit",
"config": { "subreddit": "news" }
Expand Down
9 changes: 6 additions & 3 deletions goosepaper/__main__.py
Expand Up @@ -17,20 +17,22 @@ def main():
subtitle = config["subtitle"] if "subtitle" in config else None
filename = multiparser.argumentOrConfig(
"output",
default=f"Goosepaper-{datetime.datetime.now().strftime('%Y-%B-%d-%H-%M')}.pdf"
default=f"Goosepaper-{datetime.datetime.now().strftime('%Y-%B-%d-%H-%M')}.pdf",
)
replace = multiparser.argumentOrConfig("replace", False)
folder = multiparser.argumentOrConfig("folder", None)
font_size = multiparser.argumentOrConfig("font_size", 14)
print(font_size)

paper = Goosepaper(story_providers=story_providers, title=title, subtitle=subtitle)

if filename.endswith(".html"):
with open(filename, "w") as fh:
fh.write(paper.to_html())
elif filename.endswith(".pdf"):
paper.to_pdf(filename)
paper.to_pdf(filename, font_size=font_size)
elif filename.endswith(".epub"):
paper.to_epub(filename)
paper.to_epub(filename, font_size=font_size)
else:
raise ValueError(f"Unknown file extension '{filename.split('.')[-1]}'.")

Expand All @@ -39,5 +41,6 @@ def main():

return 0


if __name__ == "__main__":
sys.exit(main())
26 changes: 14 additions & 12 deletions goosepaper/goosepaper.py
Expand Up @@ -2,7 +2,7 @@
import os

from uuid import uuid4
from typing import List
from typing import List, Type
from ebooklib import epub

from .styles import Style, AutumnStyle
Expand All @@ -21,9 +21,7 @@ def __init__(
):
self.story_providers = story_providers
self.title = title if title else "Daily Goosepaper"
self.subtitle = (
subtitle + "\n" if subtitle else ""
)
self.subtitle = subtitle + "\n" if subtitle else ""
self.subtitle += datetime.datetime.today().strftime("%B %d, %Y")

def get_stories(self, deduplicate: bool = False):
Expand Down Expand Up @@ -90,7 +88,9 @@ def to_html(self) -> str:
</html>
"""

def to_pdf(self, filename: str, style: Style = AutumnStyle) -> str:
def to_pdf(
self, filename: str, style: Type[Style] = AutumnStyle, font_size: int = 14
) -> str:
"""
Renders the current Goosepaper to a PDF file on disk.
Expand All @@ -99,14 +99,16 @@ def to_pdf(self, filename: str, style: Style = AutumnStyle) -> str:
"""
from weasyprint import HTML, CSS

style = style()
style_obj = style()
html = self.to_html()
h = HTML(string=html)
c = CSS(string=style.get_css())
h.write_pdf(filename, stylesheets=[c, *style.get_stylesheets()])
c = CSS(string=style_obj.get_css(font_size))
h.write_pdf(filename, stylesheets=[c, *style_obj.get_stylesheets()])
return filename

def to_epub(self, filename: str, style: Style = AutumnStyle) -> str:
def to_epub(
self, filename: str, style: Type[Style] = AutumnStyle, font_size: int = 14
) -> str:
"""
Render the current Goosepaper to an epub file on disk
"""
Expand All @@ -129,12 +131,12 @@ def to_epub(self, filename: str, style: Style = AutumnStyle) -> str:
book.set_title(title)
book.set_language("en")

style = Style()
style_obj = Style()
css = epub.EpubItem(
uid="style_default",
file_name="style/default.css",
media_type="text/css",
content=style.get_css(),
content=style_obj.get_css(font_size),
)
book.add_item(css)

Expand Down Expand Up @@ -167,5 +169,5 @@ def to_epub(self, filename: str, style: Style = AutumnStyle) -> str:
book.add_item(epub.EpubNav())
book.spine = ["nav"] + chapters

print(f"Honk! Writing out epub {filename}")
epub.write_epub(filename, book)
return filename
3 changes: 2 additions & 1 deletion goosepaper/storyprovider.py
@@ -1,4 +1,5 @@
import abc
from typing import List
from .story import Story


Expand All @@ -7,7 +8,7 @@ class StoryProvider(abc.ABC):
An abstract class for a class that provides stories to be rendered.
"""

def get_stories(self, limit: int = 5):
def get_stories(self, limit: int = 5) -> List["Story"]:
"""
Get a list of stories from this Provider.
"""
Expand Down

0 comments on commit 44a7e60

Please sign in to comment.