mirror of
https://github.com/drewcassidy/yaclog.git
synced 2024-09-01 14:58:58 +00:00
Compare commits
17 Commits
Author | SHA1 | Date | |
---|---|---|---|
ddfd96193d | |||
dc5cc2ddd9 | |||
57542e228e | |||
13ddc5a1f9 | |||
98c21e4078 | |||
fb35ad3b29 | |||
849438a5f5 | |||
fa97e9154b | |||
f6f7a8b500 | |||
7b694bc3c0 | |||
2e2a5834e6 | |||
fbdd3f8971 | |||
9ee8096e33 | |||
b5c4a1757e | |||
1b263ad38f | |||
a900679eb6 | |||
c999822bd0 |
15
CHANGELOG.md
15
CHANGELOG.md
@ -1,9 +1,24 @@
|
|||||||
# Changelog
|
# Changelog
|
||||||
All notable changes to this project will be documented in this file
|
All notable changes to this project will be documented in this file
|
||||||
|
|
||||||
|
## 0.2.0
|
||||||
|
|
||||||
|
### Added
|
||||||
|
|
||||||
|
- New yak log logo drawn by my sister
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
|
||||||
|
- Updated package metadata
|
||||||
|
- Rewrote parser to use a 2-step method that is more flexible.
|
||||||
|
- Parser can now handle code blocks.
|
||||||
|
- Parser can now handle setext-style headers and H2s not conforming to the
|
||||||
|
schema.
|
||||||
|
|
||||||
## 0.1.0 - 2021-04-16
|
## 0.1.0 - 2021-04-16
|
||||||
|
|
||||||
First release
|
First release
|
||||||
|
|
||||||
### Added
|
### Added
|
||||||
|
|
||||||
- `yaclog.read()` method to parse changelog files
|
- `yaclog.read()` method to parse changelog files
|
||||||
|
@ -1,2 +1,6 @@
|
|||||||
# yet-another-changelog
|
# Yaclog
|
||||||
Yet another changelog command line tool
|
Yet another changelog command line tool
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
*Logo by Erin Cassidy*
|
@ -7,7 +7,6 @@ license = AGPLv3
|
|||||||
license_file = LICENSE.md
|
license_file = LICENSE.md
|
||||||
long_description = file: README.md
|
long_description = file: README.md
|
||||||
long_description_content_type = text/markdown
|
long_description_content_type = text/markdown
|
||||||
url = https://github.com/drewcassidy/yet-another-changelog
|
|
||||||
|
|
||||||
keywords = changelog, commandline, markdown
|
keywords = changelog, commandline, markdown
|
||||||
classifiers =
|
classifiers =
|
||||||
@ -23,7 +22,11 @@ classifiers =
|
|||||||
Topic :: Software Development :: Version Control :: Git
|
Topic :: Software Development :: Version Control :: Git
|
||||||
Topic :: Utilities
|
Topic :: Utilities
|
||||||
|
|
||||||
|
project_urls =
|
||||||
|
Source Code = https://github.com/drewcassidy/yaclog
|
||||||
|
Changelog = https://github.com/drewcassidy/yaclog/blob/main/CHANGELOG.md
|
||||||
|
|
||||||
[options]
|
[options]
|
||||||
install_requires = Click; GitPython
|
install_requires = Click; GitPython
|
||||||
python_requires >= 3.8
|
python_requires = >= 3.8
|
||||||
packages = find:
|
packages = find:
|
||||||
|
@ -1,3 +1,19 @@
|
|||||||
|
# yaclog: yet another changelog tool
|
||||||
|
# Copyright (c) 2021. Andrew Cassidy
|
||||||
|
#
|
||||||
|
# This program is free software: you can redistribute it and/or modify
|
||||||
|
# it under the terms of the GNU Affero General Public License as
|
||||||
|
# published by the Free Software Foundation, either version 3 of the
|
||||||
|
# License, or (at your option) any later version.
|
||||||
|
#
|
||||||
|
# This program is distributed in the hope that it will be useful,
|
||||||
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
# GNU Affero General Public License for more details.
|
||||||
|
#
|
||||||
|
# You should have received a copy of the GNU Affero General Public License
|
||||||
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
import datetime
|
import datetime
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
@ -7,6 +23,13 @@ from typing import List, Tuple, Optional
|
|||||||
bullets = '+-*'
|
bullets = '+-*'
|
||||||
brackets = '[]'
|
brackets = '[]'
|
||||||
|
|
||||||
|
code_regex = re.compile(r'^```')
|
||||||
|
header_regex = re.compile(r'^(?P<hashes>#+)\s+(?P<contents>[^#]+)(?:\s+#+)?$')
|
||||||
|
under1_regex = re.compile(r'^=+\s*$')
|
||||||
|
under2_regex = re.compile(r'^-+\s*$')
|
||||||
|
bullet_regex = re.compile(r'^[-+*]')
|
||||||
|
linkid_regex = re.compile(r'^\[(?P<link_id>\S*)]:\s*(?P<link>.*)')
|
||||||
|
|
||||||
|
|
||||||
def _strip_link(token):
|
def _strip_link(token):
|
||||||
if link_literal := re.fullmatch(r'\[(.*?)]\((.*?)\)', token):
|
if link_literal := re.fullmatch(r'\[(.*?)]\((.*?)\)', token):
|
||||||
@ -20,6 +43,22 @@ def _strip_link(token):
|
|||||||
return token, None, None
|
return token, None, None
|
||||||
|
|
||||||
|
|
||||||
|
def _join_markdown(segments: List[str]) -> str:
|
||||||
|
text: List[str] = []
|
||||||
|
last_bullet = False
|
||||||
|
for segment in segments:
|
||||||
|
is_bullet = bullet_regex.match(segment) and '\n' not in segment
|
||||||
|
|
||||||
|
if not is_bullet or not last_bullet:
|
||||||
|
text.append('')
|
||||||
|
|
||||||
|
text.append(segment)
|
||||||
|
|
||||||
|
last_bullet = is_bullet
|
||||||
|
|
||||||
|
return '\n'.join(text).strip()
|
||||||
|
|
||||||
|
|
||||||
class VersionEntry:
|
class VersionEntry:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.sections = {'': []}
|
self.sections = {'': []}
|
||||||
@ -27,17 +66,24 @@ class VersionEntry:
|
|||||||
self.date: Optional[datetime.date] = None
|
self.date: Optional[datetime.date] = None
|
||||||
self.tags: List[str] = []
|
self.tags: List[str] = []
|
||||||
self.link: str = ''
|
self.link: str = ''
|
||||||
|
self.link_id: str = None
|
||||||
|
self.line_no: int = -1
|
||||||
|
|
||||||
def __str__(self) -> str:
|
def __str__(self) -> str:
|
||||||
if self.name.lower() == 'unreleased':
|
if self.link:
|
||||||
return f'## {self.name}'
|
segments = [f'[{self.name}]']
|
||||||
|
else:
|
||||||
|
segments = [self.name]
|
||||||
|
|
||||||
date_str = self.date.isoformat() if self.date else 'UNKNOWN'
|
if self.date or len(self.tags) > 0:
|
||||||
line = f'## {self.name} - {date_str}'
|
segments.append('-')
|
||||||
for tag in self.tags:
|
|
||||||
line += ' [' + tag.upper() + ']'
|
|
||||||
|
|
||||||
return line
|
if self.date:
|
||||||
|
segments.append(self.date.isoformat())
|
||||||
|
|
||||||
|
segments += [f'[{t.upper()}]' for t in self.tags]
|
||||||
|
|
||||||
|
return ' '.join(segments)
|
||||||
|
|
||||||
|
|
||||||
class Changelog:
|
class Changelog:
|
||||||
@ -45,92 +91,168 @@ class Changelog:
|
|||||||
self.path = path
|
self.path = path
|
||||||
self.header = ''
|
self.header = ''
|
||||||
self.versions = []
|
self.versions = []
|
||||||
|
|
||||||
|
# Read file
|
||||||
|
with open(path, 'r') as fp:
|
||||||
|
self.lines = fp.readlines()
|
||||||
|
|
||||||
|
section = ''
|
||||||
|
in_block = False
|
||||||
|
in_code = False
|
||||||
|
|
||||||
self.links = {}
|
self.links = {}
|
||||||
|
|
||||||
with open(path, 'r') as fp:
|
links = {}
|
||||||
# Read file
|
segments: List[Tuple[int, List[str], str]] = []
|
||||||
line = fp.readline()
|
header_segments = []
|
||||||
while line and not line.startswith('##'):
|
|
||||||
self.header += line
|
|
||||||
line = fp.readline()
|
|
||||||
|
|
||||||
version = None
|
for line_no, line in enumerate(self.lines):
|
||||||
section = ''
|
if in_code:
|
||||||
last_line = ''
|
# this is the contents of a code block
|
||||||
|
segments[-1][1].append(line)
|
||||||
|
if code_regex.match(line):
|
||||||
|
in_code = False
|
||||||
|
in_block = False
|
||||||
|
|
||||||
while line:
|
elif code_regex.match(line):
|
||||||
if line.isspace():
|
# this is the start of a code block
|
||||||
# skip empty lines
|
in_code = True
|
||||||
pass
|
segments.append((line_no, [line], 'code'))
|
||||||
elif match := re.fullmatch(
|
|
||||||
r'^##\s+(?P<name>\S*)(?:\s+-\s+(?P<date>\S+))?\s*?(?P<extra>.*?)\s*#*$', line):
|
|
||||||
# this is a version header in the form '## Name (- date) (tags*) (#*)'
|
|
||||||
version = VersionEntry()
|
|
||||||
section = ''
|
|
||||||
|
|
||||||
version.name, version.link, version.link_id = _strip_link(match['name'])
|
elif under1_regex.match(line) and in_block and len(segments[-1][1]) == 1 and segments[-1][2] == 'p':
|
||||||
|
# this is an underline for a setext-style H1
|
||||||
|
# ugly but it works
|
||||||
|
last = segments.pop()
|
||||||
|
segments.append((last[0], last[1] + [line], 'h1'))
|
||||||
|
|
||||||
if match['date']:
|
elif under2_regex.match(line) and in_block and len(segments[-1][1]) == 1 and segments[-1][2] == 'p':
|
||||||
|
# this is an underline for a setext-style H2
|
||||||
|
# ugly but it works
|
||||||
|
last = segments.pop()
|
||||||
|
segments.append((last[0], last[1] + [line], 'h2'))
|
||||||
|
|
||||||
|
elif bullet_regex.match(line):
|
||||||
|
in_block = True
|
||||||
|
segments.append((line_no, [line], 'li'))
|
||||||
|
|
||||||
|
elif match := header_regex.match(line):
|
||||||
|
# this is a header
|
||||||
|
kind = f'h{len(match["hashes"])}'
|
||||||
|
segments.append((line_no, [line], kind))
|
||||||
|
in_block = False
|
||||||
|
|
||||||
|
elif match := linkid_regex.match(line):
|
||||||
|
# this is a link definition in the form '[id]: link', so add it to the link table
|
||||||
|
links[match['link_id'].lower()] = match['link']
|
||||||
|
|
||||||
|
elif line.isspace():
|
||||||
|
# skip empty lines
|
||||||
|
in_block = False
|
||||||
|
|
||||||
|
elif in_block:
|
||||||
|
# this is a line to be added to a paragraph
|
||||||
|
segments[-1][1].append(line)
|
||||||
|
else:
|
||||||
|
# this is a new paragraph
|
||||||
|
in_block = True
|
||||||
|
segments.append((line_no, [line], 'p'))
|
||||||
|
|
||||||
|
for segment in segments:
|
||||||
|
text = ''.join(segment[1]).strip()
|
||||||
|
|
||||||
|
if segment[2] == 'h2':
|
||||||
|
# start of a version
|
||||||
|
|
||||||
|
slug = text.rstrip('-').strip('#').strip()
|
||||||
|
split = slug.split()
|
||||||
|
if '-' in split:
|
||||||
|
split.remove('-')
|
||||||
|
|
||||||
|
version = VersionEntry()
|
||||||
|
section = ''
|
||||||
|
|
||||||
|
version.name = slug
|
||||||
|
version.line_no = segment[0]
|
||||||
|
tags = []
|
||||||
|
date = []
|
||||||
|
|
||||||
|
for word in split[1:]:
|
||||||
|
if match := re.match(r'\d{4}-\d{2}-\d{2}', word):
|
||||||
|
# date
|
||||||
try:
|
try:
|
||||||
version.date = datetime.date.fromisoformat(match['date'].strip(string.punctuation))
|
date = datetime.date.fromisoformat(match[0])
|
||||||
except ValueError:
|
except ValueError:
|
||||||
version.date = None
|
break
|
||||||
|
elif match := re.match(r'^\[(?P<tag>\S*)]', word):
|
||||||
if match['extra']:
|
tags.append(match['tag'])
|
||||||
version.tags = [s.strip('[]') for s in re.findall(r'\[.*?]', match['extra'])]
|
else:
|
||||||
|
break
|
||||||
self.versions.append(version)
|
|
||||||
|
|
||||||
elif match := re.fullmatch(r'###\s+(\S*?)(\s+#*)?', line):
|
|
||||||
# this is a version section header in the form '### Name' or '### Name ###'
|
|
||||||
section = match[1].title()
|
|
||||||
if section not in version.sections.keys():
|
|
||||||
version.sections[section] = []
|
|
||||||
|
|
||||||
elif match := re.fullmatch(r'\[(\S*)]:\s*(\S*)\n', line):
|
|
||||||
# this is a link definition in the form '[id]: link', so add it to the link table
|
|
||||||
self.links[match[1].lower()] = match[2]
|
|
||||||
|
|
||||||
elif line[0] in bullets or last_line.isspace():
|
|
||||||
# bullet point or new paragraph
|
|
||||||
# bullet points are preserved since some people like to use '+', '-' or '*' for different things
|
|
||||||
version.sections[section].append(line.strip())
|
|
||||||
|
|
||||||
else:
|
else:
|
||||||
# not a bullet point, and no whitespace on last line, so append to the last entry
|
# matches the schema
|
||||||
version.sections[section][-1] += '\n' + line.strip()
|
version.name, version.link, version.link_id = _strip_link(split[0])
|
||||||
|
version.date = date
|
||||||
|
version.tags = tags
|
||||||
|
|
||||||
last_line = line
|
self.versions.append(version)
|
||||||
line = fp.readline()
|
|
||||||
|
elif len(self.versions) == 0:
|
||||||
|
# we haven't encountered any version headers yet,
|
||||||
|
# so its best to just add this line to the header string
|
||||||
|
header_segments.append(text)
|
||||||
|
|
||||||
|
elif segment[2] == 'h3':
|
||||||
|
# start of a version section
|
||||||
|
section = text.strip('#').strip()
|
||||||
|
if section not in self.versions[-1].sections.keys():
|
||||||
|
self.versions[-1].sections[section] = []
|
||||||
|
|
||||||
|
else:
|
||||||
|
# change log entry
|
||||||
|
self.versions[-1].sections[section].append(text)
|
||||||
|
|
||||||
|
# handle links
|
||||||
|
for version in self.versions:
|
||||||
|
if match := re.fullmatch(r'\[(.*)]', version.name):
|
||||||
|
# ref-matched link
|
||||||
|
link_id = match[1].lower()
|
||||||
|
if link_id in links:
|
||||||
|
version.link = links.pop(link_id)
|
||||||
|
version.link_id = None
|
||||||
|
version.name = match[1]
|
||||||
|
|
||||||
|
elif version.link_id in links:
|
||||||
|
# id-matched link
|
||||||
|
version.link = links.pop(version.link_id)
|
||||||
|
|
||||||
|
# strip whitespace from header
|
||||||
|
self.header = _join_markdown(header_segments)
|
||||||
|
self.links = links
|
||||||
|
|
||||||
|
def write(self, path: os.PathLike = None):
|
||||||
|
if path is None:
|
||||||
|
path = self.path
|
||||||
|
|
||||||
|
v_links = {}
|
||||||
|
v_links.update(self.links)
|
||||||
|
|
||||||
|
with open(path, 'w') as fp:
|
||||||
|
fp.write(self.header)
|
||||||
|
fp.write('\n\n')
|
||||||
|
|
||||||
for version in self.versions:
|
for version in self.versions:
|
||||||
# handle links
|
fp.write(f'## {version}\n\n')
|
||||||
if match := re.fullmatch(r'\[(.*)]', version.name):
|
|
||||||
# ref-matched link
|
|
||||||
link_id = match[1].lower()
|
|
||||||
if link_id in self.links:
|
|
||||||
version.link = self.links.pop(link_id)
|
|
||||||
version.link_id = None
|
|
||||||
version.name = match[1]
|
|
||||||
|
|
||||||
elif version.link_id in self.links:
|
if version.link:
|
||||||
# id-matched link
|
v_links[version.name] = version.link
|
||||||
version.link = self.links.pop(version.link_id)
|
|
||||||
|
|
||||||
|
for section in version.sections:
|
||||||
|
if section:
|
||||||
|
fp.write(f'### {section}\n\n')
|
||||||
|
|
||||||
def read_version_header(line: str) -> Tuple[str, datetime.date, List[str]]:
|
if len(version.sections[section]) > 0:
|
||||||
split = line.removeprefix('##').strip().split()
|
fp.write(_join_markdown(version.sections[section]))
|
||||||
name = split[0]
|
fp.write('\n\n')
|
||||||
date = datetime.date.fromisoformat(split[2]) if len(split) > 2 else None
|
|
||||||
tags = [s.removeprefix('[').removesuffix(']') for s in split[3:]]
|
|
||||||
|
|
||||||
return name, date, tags
|
for link_id, link in v_links.items():
|
||||||
|
fp.write(f'[{link_id.lower()}]: {link}\n')
|
||||||
|
|
||||||
def write_version_header(name: str, date: datetime.date, tags=None) -> str:
|
|
||||||
line = f'## {name} - {date.isoformat()}'
|
|
||||||
if tags:
|
|
||||||
for tag in tags:
|
|
||||||
line += ' [' + tag.upper() + ']'
|
|
||||||
|
|
||||||
return line
|
|
||||||
|
Reference in New Issue
Block a user