aoc_generator 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +7 -1
- data/Gemfile.lock +1 -1
- data/README.md +3 -4
- data/lib/advent_of_code_generator/cli.rb +4 -1
- data/lib/advent_of_code_generator/generator.rb +5 -5
- data/lib/advent_of_code_generator/html_parser.rb +50 -0
- data/lib/advent_of_code_generator/scraper.rb +4 -2
- data/lib/advent_of_code_generator.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4b769226917de985f62166b06a18ac48433df5e5ca362fdfc7248338d368ce39
|
4
|
+
data.tar.gz: 9f1be6d33fe107041e8ebcbc90c9dc67b05d5ad27506a84ee2ebe5239cf4920b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0fad21368f7c4033a73670fdb2dece5a1ed468de3e0075d50426b821d65a3e15ae840bf2c8f6e20f5e8070f836ffe21bb72eb7abd100162bed5ee60101ca8821
|
7
|
+
data.tar.gz: 1ec320f03139f386435f78e21f3922e840a36433795f7e34421ff4d464b53073497c7c742da1998a025b15e31c1c34e719a5f008cd049a8ad65d2351f1ad8234
|
data/CHANGELOG.md
CHANGED
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -16,12 +16,11 @@ A Ruby gem that generates daily puzzle templates for Advent of Code.
|
|
16
16
|
|
17
17
|
```sh
|
18
18
|
-y, [--year=N] # Defaults to the current year.
|
19
|
-
# Default: 2024
|
20
19
|
-d, [--day=N] # Defaults to the current day.
|
21
|
-
# Default: 28
|
22
20
|
-u, [--username=USERNAME] # Files will be generated in a directory with this name. Useful for multi-user repos.
|
23
21
|
# Default: advent_of_code
|
24
22
|
-s, [--session=SESSION] # Your adventofcode.com session key. Necessary for scraping data files and specs for part two.
|
23
|
+
# Default: reads AOC_SESSION environment variable if it exists.
|
25
24
|
```
|
26
25
|
|
27
26
|
## Generated file structure
|
@@ -32,8 +31,8 @@ adventofcode/
|
|
32
31
|
└── year_2024/
|
33
32
|
|
|
34
33
|
└── day_01/
|
35
|
-
├──
|
36
|
-
├──
|
34
|
+
├── day_01.rb
|
35
|
+
├── day_01_spec.rb
|
37
36
|
├── data.txt
|
38
37
|
└── README.md
|
39
38
|
```
|
@@ -32,7 +32,10 @@ module AdventOfCodeGenerator
|
|
32
32
|
method_option :session,
|
33
33
|
required: false,
|
34
34
|
aliases: "-s",
|
35
|
-
desc: "Your adventofcode.com session key.
|
35
|
+
desc: "Your adventofcode.com session key. " \
|
36
|
+
"Necessary for scraping data files and specs for part two. " \
|
37
|
+
"Defaults to a AOC_SESSION environment variable if it exists.",
|
38
|
+
default: ENV.fetch("AOC_SESSION", nil)
|
36
39
|
|
37
40
|
def generate
|
38
41
|
scraper = AdventOfCodeGenerator::Scraper.new(options)
|
@@ -12,8 +12,8 @@ module AdventOfCodeGenerator
|
|
12
12
|
# └── year_2024/
|
13
13
|
# |
|
14
14
|
# └── day_01/
|
15
|
-
# ├──
|
16
|
-
# ├──
|
15
|
+
# ├── day_01.rb
|
16
|
+
# ├── day_01_spec.rb
|
17
17
|
# ├── data.txt
|
18
18
|
# └── README.md
|
19
19
|
#
|
@@ -58,7 +58,7 @@ module AdventOfCodeGenerator
|
|
58
58
|
end
|
59
59
|
|
60
60
|
def main_file
|
61
|
-
path = "#{daily_directory}/
|
61
|
+
path = "#{daily_directory}/day_#{@day}.rb"
|
62
62
|
content = <<~RUBY
|
63
63
|
# frozen_string_literal: true
|
64
64
|
|
@@ -85,11 +85,11 @@ module AdventOfCodeGenerator
|
|
85
85
|
end
|
86
86
|
|
87
87
|
def spec_file
|
88
|
-
path = "#{daily_directory}/
|
88
|
+
path = "#{daily_directory}/day_#{@day}_spec.rb"
|
89
89
|
content = <<~RUBY
|
90
90
|
# frozen_string_literal: true
|
91
91
|
|
92
|
-
require_relative "
|
92
|
+
require_relative "day_#{@day}"
|
93
93
|
|
94
94
|
RSpec.describe #{@username.capitalize}::Year#{@year}::Day#{@day} do
|
95
95
|
subject(:puzzle) { described_class.new(input) }
|
@@ -0,0 +1,50 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "nokogiri"
|
4
|
+
|
5
|
+
module AdventOfCodeGenerator
|
6
|
+
# Converts HTML puzzle descriptions from adventofcode.com into markdown format.
|
7
|
+
class HTMLParser
|
8
|
+
def initialize(input)
|
9
|
+
@input = input
|
10
|
+
end
|
11
|
+
|
12
|
+
def call
|
13
|
+
articles.map do |node|
|
14
|
+
process_article(node)
|
15
|
+
end.join("\n").strip
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def articles
|
21
|
+
doc = Nokogiri::HTML(@input)
|
22
|
+
doc.css("article")
|
23
|
+
end
|
24
|
+
|
25
|
+
def process_article(article)
|
26
|
+
article.children.map { |node| process_node(node) }.compact
|
27
|
+
end
|
28
|
+
|
29
|
+
def process_node(node)
|
30
|
+
case node.name
|
31
|
+
when "h2"
|
32
|
+
"## #{node.text}\n"
|
33
|
+
when "p"
|
34
|
+
"#{process_paragraph(node)}\n"
|
35
|
+
when "pre"
|
36
|
+
"```sh\n#{node.text.strip}\n```\n"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def process_paragraph(para)
|
41
|
+
para.inner_html
|
42
|
+
.gsub(%r{<a[^>]*href="([^"]*)"[^>]*>(.*?)</a>}, '[\2](\1)')
|
43
|
+
.gsub(%r{<code><em>(.*?)</em></code>}, '**`\1`**')
|
44
|
+
.gsub(%r{<em><code>(.*?)</code></em>}, '**`\1`**')
|
45
|
+
.gsub(%r{<em.*?>(.*?)</em>}, '**\1**')
|
46
|
+
.gsub(%r{<code>(.*?)</code>}, '`\1`')
|
47
|
+
.gsub(/<[^>]*>/, "")
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -1,14 +1,16 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require "net/http"
|
4
|
+
require_relative "html_parser"
|
4
5
|
|
5
6
|
module AdventOfCodeGenerator
|
6
7
|
# Fetches puzzle descriptions and input data from adventofcode.com.
|
7
8
|
class Scraper
|
8
|
-
def initialize(options)
|
9
|
+
def initialize(options, parser = AdventOfCodeGenerator::HTMLParser)
|
9
10
|
@year = options[:year]
|
10
11
|
@day = options[:day]
|
11
12
|
@session_key = options[:session]
|
13
|
+
@parser = parser
|
12
14
|
end
|
13
15
|
|
14
16
|
def puzzle_description
|
@@ -17,7 +19,7 @@ module AdventOfCodeGenerator
|
|
17
19
|
uri = URI("https://adventofcode.com/#{@year}/day/#{@day}")
|
18
20
|
response = Net::HTTP.get_response(uri, headers)
|
19
21
|
|
20
|
-
response.body
|
22
|
+
@parser.new(response.body).call
|
21
23
|
end
|
22
24
|
|
23
25
|
def input_data
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: aoc_generator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Daniel Nguyen
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-11-
|
11
|
+
date: 2024-11-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: fileutils
|
@@ -74,6 +74,7 @@ files:
|
|
74
74
|
- lib/advent_of_code_generator.rb
|
75
75
|
- lib/advent_of_code_generator/cli.rb
|
76
76
|
- lib/advent_of_code_generator/generator.rb
|
77
|
+
- lib/advent_of_code_generator/html_parser.rb
|
77
78
|
- lib/advent_of_code_generator/scraper.rb
|
78
79
|
homepage: https://github.com/cheddachedda/advent_of_code_generator
|
79
80
|
licenses: []
|