-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathRakefile
112 lines (92 loc) · 2.85 KB
/
Rakefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
require 'rake'
require 'net/http'
require 'json'
require 'yaml'
require 'fileutils'
require 'nokogiri'
task :generate_jobs do
# Replace with your actual Lever.co API endpoint
lever_api_endpoint = 'https://api.lever.co/v0/postings/NJStateOfficeofInnovation'
uri = URI(lever_api_endpoint)
dir_path = File.join(__dir__, 'content', '_join')
FileUtils.mkdir_p(dir_path) unless Dir.exist?(dir_path)
begin
response = Net::HTTP.get(uri)
jobs = JSON.parse(response)
puts "Fetched #{jobs.size} jobs from Lever API"
jobs.each do |job|
title = job['text']
slug = title.downcase.strip.gsub(' ', '-').gsub(/[^\w-]/, '')
front_matter = {
'title' => title,
'team' => job['categories']['team'],
'commitment' => job['categories']['commitment'],
'department' => job['categories']['department'],
'applyURL' => job['applyUrl'],
'published' => true
}
content = <<~EOS
#{front_matter.to_yaml}---
#{build_job_listing_html(job)}
EOS
file_path = File.join(dir_path, "#{slug}.md")
File.open(file_path, 'w') { |file| file.write(content) }
puts "Created #{file_path}"
end
rescue => e
puts "Error fetching Lever JSON data: #{e.message}"
end
end
def build_job_listing_html(job)
job_listing = ""
if job['description']
job_listing += format_html_to_markdown(job['description'])
job_listing += "\n"
end
if job['lists']
job['lists'].each do |list|
if list && list.key?('text') && list.key?('content')
job_list_markdown = format_html_to_markdown(list['content'])
job_listing += "\n### #{list['text']}\n#{job_list_markdown}\n"
end
end
end
if job['additional']
job_listing += format_html_to_markdown(job['additional'])
end
job_listing
end
def format_html_to_markdown(text)
# Parse the HTML text with Nokogiri
doc = Nokogiri::HTML::DocumentFragment.parse(text)
# Process each <div> element
doc.css('div').each do |div|
# Check for <b> tags with specific style and replace with markdown headers
div.css('b[style="font-size: 18px"]').each do |b|
b.replace("## #{b.text.strip}\n")
end
# Replace <br> tags with newlines
div.css('br').each do |br|
br.replace("\n")
end
# Append a newline after each <div>
div.add_next_sibling(Nokogiri::XML::Text.new("\n", doc))
end
# Handle <a> tags to preserve links in Markdown format
doc.css('a').each do |a|
a.replace("[#{a.text}](#{a['href']})")
end
# Preserve bold text and convert underline to bold
doc.css('b, u').each do |b|
b.replace("**#{b.text.strip}**")
end
# Preserve italicized text
doc.css('i, em').each do |i|
i.replace("*#{i.text.strip}*")
end
# Process <li> elements for markdown list formatting
doc.css('li').each do |li|
li.replace("- #{li.text.strip}\n")
end
doc.text
end