1
0
mirror of https://github.com/projekteuler/projekteuler.git synced 2026-01-26 18:18:51 +01:00

Prefill original title and content from projecteuler.net

This commit is contained in:
2020-05-02 20:18:04 +02:00
parent 3e85290fc6
commit 38633d6e79
21 changed files with 178 additions and 72 deletions

View File

@@ -1,16 +1,11 @@
class Admin::DashboardController < AdminController
def index
@current_problem_count = Problem.count
@most_recent_pull = Problem.maximum(:pulled_at)
end
def update_problem_count
begin
new_problem_count = params[:problem_count].to_i
raise t('no_problem_count') unless new_problem_count
Problem.update_count(new_problem_count)
redirect_to({:controller => 'admin/dashboard', :action => :index}, notice: t('.success_message'))
rescue => e
redirect_to({:controller => 'admin/dashboard', :action => :index}, alert: t('.failure_message', error: e.message))
end
def pull_problems
PullProblemsJob.perform_later
redirect_to({:controller => 'admin/dashboard', :action => :index}, notice: t('.pull_problems_initiated'))
end
end

View File

@@ -7,9 +7,6 @@ class ProblemsController < ApplicationController
end
def show
unless @problem.is_translated?
render action: "untranslated"
end
end
private

View File

@@ -8,6 +8,9 @@ class TranslationsController < ApplicationController
if @problem.is_translated?
@translation.title = @problem.translation.title
@translation.content = @problem.translation.content
else
@translation.title = @problem.original_title
@translation.content = @problem.original_content
end
end

View File

@@ -0,0 +1,21 @@
require 'open-uri'
class PullProblemContentJob < ApplicationJob
queue_as :default
def perform(problem)
html = URI.open("https://projecteuler.net/minimal=#{problem.id}").read
html.strip!
# Linked problems
html.gsub!('<a href="problem=', '<a href="/problem=')
# Linked about pages
html.gsub!('<a href="about=', '<a href="/about=')
# Linked txt resources
html.gsub!('<a href="project/resources/', '<a href="https://projecteuler.net/project/resources/')
# Included images
html.gsub!('<img src="project/images/', '<img src="https://projecteuler.net/project/images/')
problem.update(original_content: html, pulled_at: Time.current())
end
end

View File

@@ -0,0 +1,23 @@
require 'csv'
require 'open-uri'
class PullProblemsJob < ApplicationJob
queue_as :default
PULL_URL = "https://projecteuler.net/minimal=problems;csv"
def perform
csv = CSV.parse(URI.open(PULL_URL), headers: [:id, :title, :date_published, :date_last_updated, :solved_by])
csv.each do |row|
id = row[:id].to_i
last_updated = Time.at(row[:date_last_updated].to_i)
problem = Problem.where(id: id).first_or_create!
if problem.pulled_at.nil? or problem.pulled_at < last_updated
original_title = row[:title]
problem.update(original_title: original_title)
# Don't update pulled_at yet until we also successfully pulled the original_content
PullProblemContentJob.perform_later problem
end
end
end
end

View File

@@ -3,9 +3,10 @@
<h1><%= t('.administration') %></h1>
</div>
<%= link_to t('.view_translations'), admin_translations_path, class: 'btn btn-primary' %>
<h1><%= t('.update_problem_count') %></h1>
<%= form_tag '/admin/update_problem_count', method: :post, class: 'form-inline' do %>
<%= number_field_tag 'problem_count', @current_problem_count, min: @current_problem_count, class: 'form-control' %>
<%= submit_tag t('.update'), class: 'btn btn-warning' %>
<% end %>
<p>
<%= t('.number_of_problems') %>: <%= @current_problem_count %><br/>
<% if @most_recent_pull.present? %>
<%= t('.time_since_last_pull') %>: <%= time_ago_in_words @most_recent_pull %>
<% end %>
</p>
<%= button_to t('.pull_problems'), '/admin/pull_problems', method: :post, class: 'btn btn-warning' %>

View File

@@ -28,12 +28,10 @@
<% if problem.is_translated? %>
<%= link_to problem.title, problem %>
<% else %>
<i><%= t 'problems.not_yet_translated' %></i>
<%= link_to new_problem_translation_path(problem), class: 'btn btn-primary btn-sm' do %>
<%= icon('fas', 'edit') %> <%= t '.suggest_translation' %>
<% end %>
<%= link_to problem do %>
<i><%= problem.original_title %> <%= t 'problems.not_translated_yet' %></i>
<% end %>
<% end %>
</td>
</tr>
<% end %>

View File

@@ -1,7 +1,13 @@
<% provide(:title, t('problems.show.problem_subtitle', id: @problem.id)) %>
<div class="pb-2 mt-4 mb-2">
<h1><%= @problem.title %></h1>
<h1>
<% if @problem.is_translated? %>
<%= @problem.title %>
<% else %>
<i><%= @problem.original_title %> <%= t 'problems.not_translated_yet' %></i>
<% end %>
</h1>
</div>
<% if Problem.exists?(@problem.id-1) %>
<%= link_to problem_path(@problem.id-1), title: t('problems.show.problem_subtitle', id: @problem.id-1), class: 'problem-prev' do %>
@@ -19,15 +25,26 @@
</div>
<div class="problem-buttons">
<%= link_to new_problem_translation_path(@problem), class: 'problem-buttons-inner btn btn-primary btn-sm' do %>
<%= icon('fas', 'edit') %> <%= t '.improve_translation' %>
<%= icon('fas', 'edit') %>
<% if @problem.is_translated? %>
<%= t '.improve_translation' %>
<% else %>
<%= t '.suggest_translation' %>
<% end %>
<% end %>
</div>
<div class="card-body problem-content">
<%= sanitize @problem.content, scrubber: TranslationContentScrubber.new %>
</div>
<div class="card-footer text-muted">
<%= render 'shared/authors', authors: @problem.authors, has_anonymous_author: @problem.has_anonymous_author? %>
<% if @problem.is_translated? %>
<%= sanitize @problem.content, scrubber: TranslationContentScrubber.new %>
<% else %>
<%= sanitize @problem.original_content, scrubber: TranslationContentScrubber.new %>
<% end %>
</div>
<% if @problem.is_translated? %>
<div class="card-footer text-muted">
<%= render 'shared/authors', authors: @problem.authors, has_anonymous_author: @problem.has_anonymous_author? %>
</div>
<% end %>
</div>
<div class="text-center">
<%= link_to t('.view_original_problem'), @problem.original_url, target: '_blank' %>

View File

@@ -1,10 +0,0 @@
<% provide(:title, t('problems.show.problem_subtitle', id: @problem.id)) %>
<div class="pb-2 mt-4 mb-2 border-bottom">
<h1><%= t 'problems.show.problem_subtitle', id: @problem.id %></h1>
</div>
<%= t 'problems.not_yet_translated' %>
<%= link_to new_problem_translation_path(@problem), class: 'btn btn-primary btn-sm' do %>
<%= icon('fas', 'edit') %> <%= t 'problems.index.suggest_translation' %>
<% end %>

View File

@@ -1,15 +1,12 @@
<div class="card">
<div class="card mb-3">
<div class="card-header">
Hinweise zur Erstellung von Übersetzungen
</div>
<div class='card-body'>
<ul>
<li>Als Basis für jede Übersetzung dient der HTML-Quelltext von projecteuler.net.
Dazu gehen Sie auf die entsprechende <%= link_to "Problem-Seite", @problem.original_url, target: '_blank' %>.
Dort öffnen Sie den HTML-Quelltext (z.B. mit <code>Strg+U</code> in Firefox) und kopieren alles zwischen <code>&lt;div class=&quot;problem_content&quot; role=&quot;problem&quot;&gt;</code> und <code>&lt;/div&gt;</code>.
Diesen Quelltext fügen Sie auf dieser Seite ein.
In Zukunft sollen diese Schritte automatisch passieren.</li>
<li>Der HTML-Quelltext sollte im Allgemeinen unverändert bleiben.</li>
<li>Als Basis für jede Übersetzung dient der <%= link_to "Text von projecteuler.net", @problem.original_url, target: '_blank' %>.</li>
<li>Der Originaltitel und Text im untenstehenden Formular sind zu übersetzen.</li>
<li>Der HTML-Quelltext in seiner Struktur sollte im Allgemeinen unverändert bleiben.</li>
<li>Querverweise zu anderen Problemen sollten in der Form <code>/problem=??</code> erfolgen.</li>
<li>Wenn Grafiken oder Dateien verwendet werden, sollte die URL <code>https://projecteuler.net/...</code> verwendet werden.</li>
<li>Die Sie-Form benutzen.</li>