til-rb 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 40d7142e1a89db2641e1d6da0699c13ee4a4da511331c9822deaef61ca9d546b
4
- data.tar.gz: 29eedc188aa255a16d0cb9734412594013b1b2a18ae2836c5194cc0a3ae827e5
3
+ metadata.gz: 36329998af769fde62b800136de45304100925992f85d14463a127070da4b66d
4
+ data.tar.gz: e0283b08a1145176a3e6b7ea6e77dc65f4030f86571d7db3f6b475a3085213fa
5
5
  SHA512:
6
- metadata.gz: d58c4a7620cfd20fc9901fa3bc10526662a6eea6c0393d58334976cf67a002a871bfad747c6b8ddd95190e824a9f98d2cd81548eebab2811cccefdda7a0d6aa6
7
- data.tar.gz: c8f88bf4d8a0336efafcbcb4d0add65a8979845fbea4e34cca19e29a2563e06668335c94d22a3af1003f5eb35782db1329de158685a7518b84131276a0ea6486
6
+ metadata.gz: '092def75bd54793b9318dc91a0b58bf3a6b09a488100daa0320bf97882f6fa94c6b84686aaf54c12e5bf51b662cc322b41de9a0a62c7e2fd2bd66732d3e80780'
7
+ data.tar.gz: 3d241da839314e320055bf5c2e717017f57deafdf2a9f3463a8174ccd4a846df3afa68c9f2601669ab5b54cd8aa034c0d864a68dff6d0a7e0136c0d48d199953
@@ -1,2 +1,3 @@
1
1
  require 'til/core'
2
+ require 'til/readme_updater'
2
3
  require 'til/version'
@@ -1,5 +1,6 @@
1
1
  require 'octokit'
2
2
  require 'tempfile'
3
+ require 'readline'
3
4
 
4
5
  module Til
5
6
  class Core
@@ -37,6 +38,9 @@ module Til
37
38
  check_environment_variables
38
39
  existing_categories = fetch_existing_categories
39
40
  selected_category = prompt_fzf(existing_categories)
41
+ if @new_category
42
+ selected_category = prompt_for_new_category
43
+ end
40
44
  prepopulate_tempfile(selected_category)
41
45
  open_editor
42
46
  til_content = read_file
@@ -99,6 +103,10 @@ module Til
99
103
  throw :exit
100
104
  end
101
105
 
106
+ def prompt_for_new_category
107
+ Readline.readline("New category > ").downcase
108
+ end
109
+
102
110
  def prepopulate_tempfile(selected_category, title = 'Title Placeholder')
103
111
  @tempfile = Tempfile.new('til.md')
104
112
  @tempfile.write("# #{title}")
@@ -155,28 +163,12 @@ module Til
155
163
  end
156
164
 
157
165
  def update_readme_content(category, commit_title, filename, readme_content)
158
- beginning = readme_content.index('### Categories') + '### Categories'.length
159
- eend = readme_content.index('---', readme_content.index('---') + 1) - 1
160
-
161
- # [["[Git](#git)", "Git", "git"], ["[Qux](#qux)", "Qux", "qux"]]
162
- categories = readme_content[beginning..eend].scan(/(\[(\w+)\]\(#(\w+)\))/)
166
+ updater = Til::ReadmeUpdater.new(readme_content)
163
167
 
164
168
  if @new_category
169
+ updater.add_item_for_new_category(category, commit_title, filename)
165
170
  else
166
- existing_cat = categories.find { |c| c[2] == category }
167
-
168
- loc_in_page = readme_content.index("### #{existing_cat[1]}")
169
- next_cat_location = readme_content.index('###', loc_in_page + 1)
170
-
171
- new_line = "- [#{commit_title}](#{category}/#{filename})"
172
- new_readme_content = ''
173
- if next_cat_location
174
- breakpoint = next_cat_location - 2
175
- new_readme_content = readme_content[0..breakpoint] + new_line + readme_content[breakpoint..]
176
- else
177
- new_readme_content = readme_content + new_line + '\n'
178
- end
179
- new_readme_content
171
+ updater.add_item_for_existing_category(category, commit_title, filename)
180
172
  end
181
173
  end
182
174
 
@@ -0,0 +1,85 @@
1
+ module Til
2
+ class ReadmeUpdater
3
+
4
+ def initialize(initial_content)
5
+ @initial_content = initial_content
6
+ end
7
+
8
+ def add_item_for_existing_category(category, item_title, filename)
9
+ beginning = @initial_content.index('### Categories') + '### Categories'.length
10
+ eend = @initial_content.index('---', @initial_content.index('---') + 1) - 1
11
+
12
+ # [["[Git](#git)", "Git", "git"], ["[Qux](#qux)", "Qux", "qux"]]
13
+ categories = @initial_content[beginning..eend].scan(/(\[(\w+)\]\(#(\w+)\))/)
14
+
15
+ existing_cat = categories.find { |c| c[2] == category }
16
+
17
+ loc_in_page = @initial_content.index("### #{existing_cat[1]}")
18
+ next_cat_location = @initial_content.index('###', loc_in_page + 1)
19
+
20
+ new_line = "- [#{item_title}](#{category}/#{filename})"
21
+ new_readme_content = ''
22
+ if next_cat_location
23
+ breakpoint = next_cat_location - 2
24
+ new_readme_content = @initial_content[0..breakpoint] + new_line + @initial_content[breakpoint..]
25
+ else
26
+ new_readme_content = @initial_content + new_line + "\n"
27
+ end
28
+ new_readme_content
29
+ end
30
+
31
+ def add_item_for_new_category(category, item_title, filename)
32
+ # TODO: We'll need some form of validation on the category name
33
+ beginning = @initial_content.index('### Categories') + '### Categories'.length
34
+ first_dashdashdash = @initial_content.index('---')
35
+ eend = @initial_content.index('---', first_dashdashdash + 1) - 1
36
+
37
+ # [["[Git](#git)", "Git", "git"], ["[Qux](#qux)", "Qux", "qux"]]
38
+ categories = @initial_content[beginning..eend].scan(/(\[(\w+)\]\(#(\w+)\))/)
39
+
40
+ insert_at = categories.bsearch_index do |category_triplet|
41
+ category_triplet[2] >= category
42
+ end
43
+
44
+ if insert_at.nil?
45
+ # It's the last category
46
+ insert_at = categories.length
47
+ end
48
+
49
+ categories.insert(insert_at, ["[#{category.capitalize}](\##{category})", category.capitalize, category])
50
+
51
+ new_categories_formatted = categories.map do |category|
52
+ "* #{category[0]}"
53
+ end.join("\n")
54
+
55
+ new_categories_formatted.prepend("### Categories\n\n")
56
+
57
+ category_sections_found = 0
58
+ current_search_index = eend + 1 + 3
59
+
60
+ while category_sections_found < insert_at
61
+ current_search_index = @initial_content.index('###', current_search_index + 1)
62
+ category_sections_found += 1
63
+ end
64
+
65
+ next_bound = @initial_content.index('###', current_search_index + 1)
66
+
67
+ new_line = "- [#{item_title}](#{category}/#{filename})"
68
+
69
+ if next_bound
70
+ new_readme_content = @initial_content[0..(first_dashdashdash + 2)] \
71
+ + "\n\n#{new_categories_formatted}\n" \
72
+ + @initial_content[eend..(next_bound - 2)] \
73
+ + "\n### #{category.capitalize}\n\n#{new_line}\n\n" \
74
+ + @initial_content[next_bound..]
75
+ else
76
+ new_readme_content = @initial_content[0..(first_dashdashdash + 2)] \
77
+ + "\n\n#{new_categories_formatted}\n" \
78
+ + @initial_content[eend..] \
79
+ + "\n### #{category.capitalize}\n\n#{new_line}\n"
80
+ end
81
+
82
+ new_readme_content
83
+ end
84
+ end
85
+ end
@@ -1,5 +1,5 @@
1
1
  module Til
2
2
 
3
- VERSION = '0.0.2'
3
+ VERSION = '0.0.3'
4
4
 
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: til-rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pierre Jambet
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: 1.11.2
41
+ - !ruby/object:Gem::Dependency
42
+ name: timecop
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.9.1
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.9.1
41
55
  description: til-rb helps you manage a repo of TILs similar to https://github.com/jbranchaud/til
42
56
  email: hello@pjam.me
43
57
  executables:
@@ -50,6 +64,7 @@ files:
50
64
  - bin/til
51
65
  - lib/til-rb.rb
52
66
  - lib/til/core.rb
67
+ - lib/til/readme_updater.rb
53
68
  - lib/til/version.rb
54
69
  homepage: https://rubygems.org/gems/til-rb
55
70
  licenses: