Skip to content

Commit

Permalink
automatically extract lichess study urls from pgns for study edits
Browse files Browse the repository at this point in the history
  • Loading branch information
ArneVogel committed Jan 2, 2021
1 parent 2e65e8d commit 95546b7
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 0 deletions.
18 changes: 18 additions & 0 deletions assets/js/remember_lichess_study.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

//We only need the site header
lines = pgn.split("\n").filter(line => line.startsWith("[Site \""));
// remove the [Site " "] from the site
lines = lines.map(site => site.split(" ")[1].replace(new RegExp("^\""), "").replace(new RegExp("\"]$"), ""));
// we only care about lichess urls
lines = lines.filter(site => site.startsWith("https://lichess.org/study/"));
// remove the chapter part
lines = lines.map(function(site) {
s = site.split("/");
s.pop(); // pop returns the popped element and updates the array
return s.join("/")
})

if (lines.length > 0) {
lichess_url = lines[0];
document.getElementById("study_lichess_study").value = lichess_url;
}
1 change: 1 addition & 0 deletions assets/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ module.exports = (env, options) => {
'blind_tactics': glob.sync('./vendor/**/*.js').concat(['./js/blind_tactics.js']),
'play_stockfish': glob.sync('./vendor/**/*.js').concat(['./js/play_stockfish.js']),
'endgames': glob.sync('./vendor/**/*.js').concat(['./js/endgames.js']),
'remember_lichess_study': glob.sync('./vendor/**/*.js').concat(['./js/remember_lichess_study.js']),
'study': glob.sync('./vendor/**/*.js').concat(['./js/study.js'])
},
output: {
Expand Down
5 changes: 5 additions & 0 deletions lib/listudy_web/controllers/study_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,12 @@ defmodule ListudyWeb.StudyController do

def edit(conn, %{"id" => id}) do
study = Studies.get_study_by_slug!(id)

{_, user} = get_user(conn)
[unique_id | _] = id |> String.split("-")
file = unique_id <> ".pgn"
{_, pgn} = File.read(get_path(file))
study = Map.put(study, :pgn, pgn)

if allowed(study, user) do
openings = Listudy.Openings.list_openings_study_form()
Expand Down
26 changes: 26 additions & 0 deletions lib/listudy_web/templates/study/edit.html.eex
Original file line number Diff line number Diff line change
@@ -1,5 +1,31 @@
<h1>Edit Study</h1>

<script <%= raw ListudyWeb.Plugs.CSP.put_nonce(@conn) %>>
/*
* "Cleans" pgn files,
* remove non standard characters
* replace some (but not all) of the html escapting
*/
function clean_pgn(pgn) {
pgn = pgn.replace(/½/g, '1/2');
pgn = pgn.replace(/&gt;/g, '>');
pgn = pgn.replace(/&lt;/g, '<');
pgn = pgn.replace(/&quot;/g, '"');
pgn = pgn.replace(/\)/g, ' ) ');
pgn = pgn.replace(/\(/g, ' ( ');
pgn = pgn.replace(/\{/g, ' { ');
pgn = pgn.replace(/\}/g, ' } ');
pgn = pgn.replace(/0-0-0/g, 'O-O-O');
pgn = pgn.replace(/0-0/g, 'O-O');
while (pgn.indexOf(" ") != -1) {
pgn = pgn.replace(/ /g, ' ');
}
return pgn;
}
var pgn = clean_pgn(`<%= javascript_escape @study.pgn %>`);
</script>
<script defer type="text/javascript" src="<%= Routes.static_path(@conn, "/js/remember_lichess_study.js") %>"></script>

<%= render "form.html", assigns: assigns, action: Routes.study_path(@conn, :update, @locale, @study) %>

<h2><%= gettext "Delete Study" %></h2>
Expand Down

0 comments on commit 95546b7

Please sign in to comment.