This repository has been archived by the owner on Jun 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathform.rb
140 lines (115 loc) · 3.46 KB
/
form.rb
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/usr/bin/env ruby
require 'rubygems'
require 'bundler'
Bundler.require(:default)
require_relative "repo"
set :bind, '0.0.0.0'
set :port, 3002
# Recaptcha configuration
Recaptcha.configure do |config|
config.public_key = ENV["RECAPTCHA_SITE"]
config.private_key = ENV["RECAPTCHA_PRIVATE"]
end
include Recaptcha::ClientHelper
include Recaptcha::Verify
# Provide authentication credentials
github = Octokit::Client.new(:access_token => ENV["LIBREIMBOT_TOKEN"])
get "/" do
redirect to("/post/")
end
get "/post/?" do
haml :post, layout_engine: :erb
end
get "/resource/?" do
haml :resource, layout_engine: :erb
end
get "/style.css" do
scss :style
end
post "/resource/?" do
# Get parameters from request
title = params[:title]
author = params[:author]
link = params[:link]
description = params[:description].empty? ? "" : " - *#{params[:description]}*"
# section = params[:section]
# category = params[:category]
repo = "libreim/awesome"
base = "gh-pages"
head = "new-resource-#{SecureRandom.uuid}"
begin
raise "No pareces humano :(" unless verify_recaptcha
raise "El título no debe estar vacío" if title.empty?
raise "El autor no debe estar vacío" if author.empty?
modify_repo repo, head, "Nuevo recurso: #{title}" do
# Add resource to unclassified
File.open("_common/unclassified.md", "a") do |file|
file.puts(if link.empty?
"**#{title}** - #{author}#{description} "
else
"[#{title} - #{author}](#{link})#{description} "
end)
end
end
# Finally, create a pull request using octokit
response = github.create_pull_request(repo, base, head, "Nuevo recurso: #{title}", "Añade *[#{title}](#{link})* de #{author}.")
@message = "Tu recurso se ha enviado. <a href=\"#{response.html_url}\">Ver pull request</a>"
haml :resource, layout_engine: :erb
rescue StandardError => e
@error = "Algo ocurrió: <em>#{e}</em>"
haml :resource, layout_engine: :erb
end
end
post "/post/?" do
# Get parameters from request
title = params[:title]
author = params[:author]
content = params[:content]
category = params[:category] || "unclassified"
date = Date.today.strftime "%Y-%m-%d"
filename = title.downcase.split(" ")[0..4].join(" ")
subs = {
"á" => "a",
"é" => "e",
"í" => "i",
"ó" => "o",
"ú" => "u",
/[^a-z ]/ => "",
" " => "-"
}
subs.each do |k, v|
filename.gsub! k, v
end
repo = "libreim/blog"
base = "gh-pages"
head = "new-post-#{filename}"
begin
raise "No pareces humano :(" unless verify_recaptcha
raise "El título no debe estar vacío" if title.empty?
raise "El autor no debe estar vacío" if author.empty?
modify_repo repo, head, "Nuevo post: #{title}" do
File.open("_posts/#{date}-#{filename}.md", "w") do |f|
f.write <<EOF
---
layout: post
title: #{title}
authors: #{author.split /,\s*/}
category: #{category}
---
#{content}
EOF
end
end
# Finally, create a pull request using octokit
response = github.create_pull_request(repo, base, head, "Nuevo post: #{title}", "Añade *#{title}* de #{author}.")
@message = "Tu post se ha enviado. <a href=\"#{response.html_url}\">Ver pull request</a>"
haml :post, layout_engine: :erb
rescue StandardError => e
@error = "Algo ocurrió: <em>#{e}</em>"
@prev_post = content
haml :post, layout_engine: :erb
end
end
post "/preview/?" do
Kramdown::Document.new(params[:content], syntax_highlighter: :rouge).to_html
end