Skip to content

Commit

Permalink
utc codes in marketing.url and keeping rendered date
Browse files Browse the repository at this point in the history
  • Loading branch information
edublancas committed Mar 25, 2024
1 parent 0e6e199 commit 2417d05
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 7 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# CHANGELOG

## 0.0.14dev
* [Feature] Keep existing date if the current file has been rendered already
* [Feature] Add utm codes to front matter url (`marketing.url`)

## 0.0.13 (2023-03-14)

Expand Down
3 changes: 2 additions & 1 deletion src/jupyblog/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,11 @@ def _render(local, cfg="jupyblog.yaml", incsource=False, log=None):
click.echo("Finished running build.sh\n\n")

click.echo("Rendering markdown...")
out_path = Path(cfg.path_to_posts_abs(), (post_name + ".md"))

mdr = MarkdownRenderer(
path_to_mds=path,
path_to_out=out_path,
img_dir=cfg.path_to_static_abs(),
img_prefix=cfg.prefix_img,
footer_template=cfg.read_footer_template(),
Expand All @@ -119,7 +121,6 @@ def _render(local, cfg="jupyblog.yaml", incsource=False, log=None):
)

out, name = mdr.render(name=name_input, include_source_in_footer=incsource)
out_path = Path(cfg.path_to_posts_abs(), (post_name + ".md"))
click.echo(f"Output: {out_path}")

# map language in code snippets if needed
Expand Down
35 changes: 32 additions & 3 deletions src/jupyblog/md.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* support for requirements.txt
* create and destroy env
"""

from copy import copy
from contextlib import contextmanager
from urllib import parse
Expand All @@ -18,7 +19,7 @@
from jupyblog.execute import ASTExecutor, extract_outputs_from_notebook_cell
from jupyblog.expand import expand
from jupyblog.exceptions import InvalidFrontMatter, InputPostException
from jupyblog.utm import add_utm_to_all_urls
from jupyblog.utm import add_utm_to_all_urls, add_utm_to_url
from jupyblog.ast import MarkdownAST, create_md_parser

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -190,6 +191,11 @@ class MarkdownRenderer:
"""
Parameters
----------
path_to_out : str or pathlib.Path
Path to the rendered version of this markdown file. Currently, it's only
used to extract the date from the file (to prevent overridding it when
rendering a new version)
img_dir : str or pathlib.Path
Output path (in the current filesystem) for images.
Expand All @@ -216,8 +222,10 @@ def __init__(
front_matter_template=None,
utm_source=None,
utm_medium=None,
path_to_out=None,
):
self.path = path_to_mds
self.path_to_out = path_to_out
self._img_dir = img_dir
self._img_prefix = img_prefix or ""
self._footer_template = footer_template
Expand Down Expand Up @@ -249,7 +257,8 @@ def render(self, name, *, include_source_in_footer, metadata=None):

md_ast = self.parser(md_raw)

# TODO: replace and use model object
# TODO: parse_metadata validates the schema, we are now using pydantic for
# models, hence, we can use it for validation and remove this
if metadata is None:
metadata = parse_metadata(md_raw)

Expand Down Expand Up @@ -303,6 +312,16 @@ def render(self, name, *, include_source_in_footer, metadata=None):
if self._front_matter_template:
metadata = {**metadata, **self._front_matter_template}

# if this has been rendered before, use the existing date
if self.path_to_out and Path(self.path_to_out).is_file():
metadata_rendered = parse_metadata(
Path(self.path_to_out).read_text(), validate=False
)
date_existing = metadata_rendered.get("date", None)

if date_existing:
metadata["date"] = date_existing

if self._footer_template:
md_out = add_footer(
md_out,
Expand All @@ -327,7 +346,17 @@ def render(self, name, *, include_source_in_footer, metadata=None):
if path and "images" not in metadata:
metadata["images"] = [path]

# TODO: extrac title from front matter and put it as H1 header
# if there is a marketing URL, add utm tags
marketing_url = metadata.get("marketing", dict()).get("url", None)

if marketing_url:
metadata["marketing"]["url"] = add_utm_to_url(
marketing_url,
source=canonical_name,
medium=self._utm_medium,
)

# TODO: extract title from front matter and put it as H1 header

md_out = replace_metadata(md_out, metadata)

Expand Down
4 changes: 3 additions & 1 deletion src/jupyblog/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ def load_front_matter_template(self, name):

now = _now()
rendered = Template(text, undefined=StrictUndefined).render(
now=now, name=name, env=os.environ
now=now,
name=name,
env=os.environ,
)
front_matter = yaml.safe_load(rendered)
else:
Expand Down
8 changes: 6 additions & 2 deletions src/jupyblog/utm.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Process URLs in a markdown file
"""

from urllib.parse import urlparse, urlencode, parse_qsl
from pathlib import PurePosixPath, Path

Expand All @@ -15,14 +16,17 @@ def find_urls(text):
return list(ast.iter_links())


def add_utm_to_url(url, source, medium, campaign):
def add_utm_to_url(url, source, medium, campaign=None):
if isinstance(url, str):
parsed = urlparse(url)
else:
parsed = url

current_params = dict(parse_qsl(parsed.query))
utm = {"utm_source": source, "utm_medium": medium, "utm_campaign": campaign}
utm = {"utm_source": source, "utm_medium": medium}

if campaign:
utm["utm_campaign"] = campaign

parsed = parsed._replace(query=urlencode({**current_params, **utm}))

Expand Down

0 comments on commit 2417d05

Please sign in to comment.