Skip to content

Commit

Permalink
refactor metadata line parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
Clément committed Jul 24, 2024
1 parent 76b00eb commit 8e01bea
Showing 1 changed file with 28 additions and 11 deletions.
39 changes: 28 additions & 11 deletions jssg/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@

from django.core.management.commands.runserver import Command as runserver

class EmptyLine(Exception) :
pass
class CommentLine(Exception) :
pass

class Document:
"""A document.
Expand Down Expand Up @@ -125,8 +130,14 @@ def load(cls, path: Path) -> "Document":

with settings.JFME_DEFAULT_METADATA_PATH.open() as f:
for line in f :
key, value = map(str.strip, re.split("[\s]", line, maxsplit=1))
metadata[key] = value
try :
# Parse a metadata key value pair
key, value = cls.parse_metadata_line(line)
metadata[key] = value
except EmptyLine : # ignore empty lines
continue
except CommentLine : # ignore comment lines
continue

with path.open() as f:
# States:
Expand All @@ -150,16 +161,14 @@ def load(cls, path: Path) -> "Document":
# Metadata end block found
state = 2
else:
if line.strip() == "": # ignore empty lines
try :
# Parse a metadata key value pair
key, value = cls.parse_metadata_line(line)
metadata[key] = value
except EmptyLine : # ignore empty lines
continue
if line.startswith("#"): # ignore comment lines
except CommentLine : # ignore comment lines
continue

# Parse a metadata key value pair
# key, value = map(str.strip, line.split("", maxsplit=1))
key, value = map(str.strip, re.split("[\s]", line, maxsplit=1))
# FIXME print("KEY {} : {} (line is: {})".format(key, value, line))
metadata[key] = value
elif state == 2:
if line.rstrip().startswith("---"):
# data end block found
Expand Down Expand Up @@ -222,7 +231,15 @@ def load_glob(
files += (p / dir).glob(glob)
# print(files)
return map(cls.load, files)


@classmethod
def parse_metadata_line(cls, line) :
if line.strip() == "": # ignore empty lines
raise EmptyLine()
if line.startswith("#"): # ignore comment lines
raise CommentLine(line)
# key, value = map(str.strip, line.split("", maxsplit=1))
return map(str.strip, re.split("[\s]", line, maxsplit=1))

class Page(Document):
"""A webpage, with a title and some content."""
Expand Down

0 comments on commit 8e01bea

Please sign in to comment.