From 0161303a2893883dd2aa58ac49f547c1b36848d7 Mon Sep 17 00:00:00 2001 From: Fabian Pankoke Date: Sun, 24 Dec 2023 14:32:32 +0100 Subject: [PATCH 1/3] added: Make the Dateformat configurable --- CHANGELOG.md | 6 + src/Parser.php | 9 +- tests/Unit/ParserTestDateTimeFormatTest.php | 31 ++++ .../ExampleChangelogDateTimeFormat.md | 141 ++++++++++++++++++ 4 files changed, 186 insertions(+), 1 deletion(-) create mode 100644 tests/Unit/ParserTestDateTimeFormatTest.php create mode 100644 tests/resources/ExampleChangelogDateTimeFormat.md diff --git a/CHANGELOG.md b/CHANGELOG.md index 07b2184..160ec28 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [Unreleased] + +### Added + +- Make the Dateformat configurable. + ## [2.1.0] - 2023-05-30 ### Added diff --git a/src/Parser.php b/src/Parser.php index 149b638..021b775 100644 --- a/src/Parser.php +++ b/src/Parser.php @@ -12,6 +12,8 @@ class Parser { + private string $dateFormat = 'Y-m-d'; + private function getNodesText(Crawler $crawler): array { return $crawler->each(function ($node) { @@ -30,7 +32,7 @@ private function parseRelease(Crawler $parsedRelease): Release $version = str_replace(['[', ']'], '', $releaseData[0]); $date = count($releaseData) >= 2 - ? Carbon::createFromFormat('Y-m-d', $releaseData[1]) + ? Carbon::createFromFormat($this->dateFormat, $releaseData[1]) : null; return new Release($version, $date); @@ -56,6 +58,11 @@ private function parseEntries(Crawler $lines) }); } + public function setDateFormat(string $dateFormat) + { + $this->dateFormat = $dateFormat; + } + public function parse(string $content): Changelog { $crawler = new Crawler($this->getChangelogHtml($content)); diff --git a/tests/Unit/ParserTestDateTimeFormatTest.php b/tests/Unit/ParserTestDateTimeFormatTest.php new file mode 100644 index 0000000..bd5b370 --- /dev/null +++ b/tests/Unit/ParserTestDateTimeFormatTest.php @@ -0,0 +1,31 @@ +setDateFormat('Y-m-d H:i'); + $this->changelog = $parser->parse($changelogContent); + } + + public function testRelease() + { + $release = $this + ->changelog + ->getLatestRelease(); + + $this->assertSame('1.0.0', $release->getVersion()); + $this->assertSame('2017-06-20 01:22', $release->getReleasedAt()->format('Y-m-d H:i')); + } +} diff --git a/tests/resources/ExampleChangelogDateTimeFormat.md b/tests/resources/ExampleChangelogDateTimeFormat.md new file mode 100644 index 0000000..9cdb33b --- /dev/null +++ b/tests/resources/ExampleChangelogDateTimeFormat.md @@ -0,0 +1,141 @@ +# Changelog +All notable changes to this project will be documented in this file. + +The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), +and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). + +## [Unreleased] + +## [1.0.0] - 2017-06-20 01:22 +### Added +- New visual identity by [@tylerfortune8](https://github.com/tylerfortune8). +- Version navigation. +- Links to latest released version in previous versions. +- "Why keep a changelog?" section. +- "Who needs a changelog?" section. +- "How do I make a changelog?" section. +- "Frequently Asked Questions" section. +- New "Guiding Principles" sub-section to "How do I make a changelog?". +- Simplified and Traditional Chinese translations from [@tianshuo](https://github.com/tianshuo). +- German translation from [@mpbzh](https://github.com/mpbzh) & [@Art4](https://github.com/Art4). +- Italian translation from [@azkidenz](https://github.com/azkidenz). +- Swedish translation from [@magol](https://github.com/magol). +- Turkish translation from [@karalamalar](https://github.com/karalamalar). +- French translation from [@zapashcanon](https://github.com/zapashcanon). +- Brazilian Portugese translation from [@Webysther](https://github.com/Webysther). +- Polish translation from [@amielucha](https://github.com/amielucha) & [@m-aciek](https://github.com/m-aciek). +- Russian translation from [@aishek](https://github.com/aishek). +- Czech translation from [@h4vry](https://github.com/h4vry). +- Slovak translation from [@jkostolansky](https://github.com/jkostolansky). +- Korean translation from [@pierceh89](https://github.com/pierceh89). +- Croatian translation from [@porx](https://github.com/porx). +- Persian translation from [@Hameds](https://github.com/Hameds). +- Ukrainian translation from [@osadchyi-s](https://github.com/osadchyi-s). + +### Changed +- Start using "changelog" over "change log" since it's the common usage. +- Start versioning based on the current English version at 0.3.0 to help + translation authors keep things up-to-date. +- Rewrite "What makes unicorns cry?" section. +- Rewrite "Ignoring Deprecations" sub-section to clarify the ideal + scenario. +- Improve "Commit log diffs" sub-section to further argument against + them. +- Merge "Why can’t people just use a git log diff?" with "Commit log + diffs" +- Fix typos in Simplified Chinese and Traditional Chinese translations. +- Fix typos in Brazilian Portuguese translation. +- Fix typos in Turkish translation. +- Fix typos in Czech translation. +- Fix typos in Swedish translation. +- Improve phrasing in French translation. +- Fix phrasing and spelling in German translation. + +### Removed +- Section about "changelog" vs "CHANGELOG". + +## [0.3.0] - 2015-12-03 01:00 +### Added +- RU translation from [@aishek](https://github.com/aishek). +- pt-BR translation from [@tallesl](https://github.com/tallesl). +- es-ES translation from [@ZeliosAriex](https://github.com/ZeliosAriex). + +## [0.2.0] - 2015-10-06 02:00 +### Changed +- Remove exclusionary mentions of "open source" since this project can + benefit both "open" and "closed" source projects equally. + +## [0.1.0] - 2015-10-06 03:00 +### Added +- Answer "Should you ever rewrite a change log?". + +### Changed +- Improve argument against commit logs. +- Start following [SemVer](https://semver.org) properly. + +## [0.0.8] - 2015-02-17 04:00 +### Changed +- Update year to match in every README example. +- Reluctantly stop making fun of Brits only, since most of the world + writes dates in a strange way. + +### Fixed +- Fix typos in recent README changes. +- Update outdated unreleased diff link. + +## [0.0.7] - 2015-02-16 05:00 +### Added +- Link, and make it obvious that date format is ISO 8601. + +### Changed +- Clarified the section on "Is there a standard change log format?". + +### Fixed +- Fix Markdown links to tag comparison URL with footnote-style links. + +## [0.0.6] - 2014-12-12 06:00 +### Added +- README section on "yanked" releases. + +## [0.0.5] - 2014-08-09 07:00 +### Added +- Markdown links to version tags on release headings. +- Unreleased section to gather unreleased changes and encourage note + keeping prior to releases. + +## [0.0.4] - 2014-08-09 08:00 +### Added +- Better explanation of the difference between the file ("CHANGELOG") + and its function "the change log". + +### Changed +- Refer to a "change log" instead of a "CHANGELOG" throughout the site + to differentiate between the file and the purpose of the file — the + logging of changes. + +### Removed +- Remove empty sections from CHANGELOG, they occupy too much space and + create too much noise in the file. People will have to assume that the + missing sections were intentionally left out because they contained no + notable changes. + +## [0.0.3] - 2014-08-09 09:00 +### Added +- "Why should I care?" section mentioning The Changelog podcast. + +## [0.0.2] - 2014-07-10 10:00 +### Added +- Explanation of the recommended reverse chronological release ordering. + +## [0.0.1] - 2014-05-31 11:00 +### Added +- This CHANGELOG file to hopefully serve as an evolving example of a + standardized open source project CHANGELOG. +- CNAME file to enable GitHub Pages custom domain +- README now contains answers to common questions about CHANGELOGs +- Good examples and basic guidelines, including proper date formatting. +- Counter-examples: "What makes unicorns cry?" + +[unreleased]: https://github.com/vdhicts/keepachangelog-parser/compare/v1.0.0...HEAD +[1.0.0]: https://github.com/vdhicts/keepachangelog-parser/compare/v0.0.1...v1.0.0 +[0.0.1]: https://github.com/vdhicts/keepachangelog-parser/releases/tag/v0.0.1 From d2aa62af073670173721542d3bfb4f05066ab98d Mon Sep 17 00:00:00 2001 From: Fabian Pankoke Date: Sun, 24 Dec 2023 14:44:14 +0100 Subject: [PATCH 2/3] changed: Make the setDateFormat function chainable. --- src/Parser.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Parser.php b/src/Parser.php index 021b775..45b6801 100644 --- a/src/Parser.php +++ b/src/Parser.php @@ -58,9 +58,11 @@ private function parseEntries(Crawler $lines) }); } - public function setDateFormat(string $dateFormat) + public function setDateFormat(string $dateFormat): Parser { $this->dateFormat = $dateFormat; + + return $this; } public function parse(string $content): Changelog From afcb0f3e256f105ca016f371fdff032790b8212d Mon Sep 17 00:00:00 2001 From: Fabian Pankoke Date: Sun, 24 Dec 2023 14:46:40 +0100 Subject: [PATCH 3/3] changed: Added docs for the setDateFormat function --- README.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/README.md b/README.md index 015b1c0..37b06b1 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,20 @@ $parser = new \Vdhicts\KeepAChangelog\Parser(); $changelog = $parser->parse($content); ``` +### Using a different date format + +The parser will parse the releases with the `Y-m-d` format by default. +If you would like to use another format, you can just pass the format to the `setDateFormat` function. + +```php +$content = file_get_contents('CHANGELOG.md'); + +$parser = new \Vdhicts\KeepAChangelog\Parser(); +$changelog = $parser + ->setDateFormat('Y-m-d H:i:s') + ->parse($content); +``` + ### Accessing the changelog The parser will return a `Changelog` models, with contains a collection of `Release` models. The `Release` model