@@ -20,17 +20,15 @@ def initialize(data)
20
20
# file: en/news/_posts/2019-12-25-ruby-2-7-0-released.md
21
21
#
22
22
def post_filename
23
- %r{\A /en/news/(?<yyyy>\d {4})/(?<mm>\d \d )/(?<dd>\d \d )/(?<name>[^/]*)/\Z } =~ post
24
-
25
- "en/news/_posts/#{ yyyy } -#{ mm } -#{ dd } -#{ name } .md"
23
+ "en/news/_posts/#{ post_date_string } -#{ post_title } .md"
26
24
end
27
25
28
26
# Returns true if the post URL does not match the expected format:
29
27
#
30
28
# /en/news/yyyy/mm/dd/ruby-2-7-0-released/
31
29
#
32
30
def post_url_invalid?
33
- !%r{ \A /en/news/ \d {4}/ \d \d / \d \d /[^/]*/ \Z } . match? ( post )
31
+ !post_url_data
34
32
end
35
33
36
34
# Returns true if the release date and the post date do not match.
@@ -50,11 +48,37 @@ def post_missing?
50
48
51
49
private
52
50
51
+ # Returns the MatchData for date and title parts of the post URL,
52
+ # or +nil+ if the post URL does not match the expected format.
53
+ #
54
+ # The MatchData includes the named captures yyyy, mm, dd, and title.
55
+ #
56
+ # Example URL:
57
+ #
58
+ # /en/news/2019/12/25/ruby-2-7-0-released/
59
+ #
60
+ def post_url_data
61
+ pattern = %r{\A /en/news/(?<yyyy>\d {4})/(?<mm>\d \d )/(?<dd>\d \d )/(?<title>[^/]*)/\z }
62
+
63
+ pattern . match ( post )
64
+ end
65
+
53
66
# The date corresponding to the given post URL.
54
67
def post_date_string
55
- %r{\A /en/news/(?<yyyy>\d {4})/(?<mm>\d \d )/(?<dd>\d \d )/(?<name>[^/]*)/\Z } =~ post
68
+ return unless post_url_data
69
+
70
+ yyyy = post_url_data [ :yyyy ]
71
+ mm = post_url_data [ :mm ]
72
+ dd = post_url_data [ :dd ]
56
73
57
74
"#{ yyyy } -#{ mm } -#{ dd } "
58
75
end
76
+
77
+ # The post title corresponding to the given post URL.
78
+ def post_title
79
+ return unless post_url_data
80
+
81
+ post_url_data [ :title ]
82
+ end
59
83
end
60
84
end
0 commit comments