Skip to content

Commit 6ede1cd

Browse files
committed
project.html now renders diff that can be copy and pasted
1 parent ec7dae9 commit 6ede1cd

File tree

1 file changed

+43
-8
lines changed

1 file changed

+43
-8
lines changed

teachprogramming/lib/static/projects.html

+43-8
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,34 @@
77
<style>
88
.hidden {display: none;}
99
code {border: 1px solid darkgrey; margin:1em; display: block; background-color: #eee;}
10+
code div {display: inline-block;}
1011
code .add {color: green; font-weight: bolder;}
1112
code .remove {color: red; text-decoration: line-through;}
12-
code .lineNum {display: inline-block; user-select: none; background-color: lightgray;}
13-
code .code {display: inline-block;}
13+
code .lineNum {user-select: none; background-color: lightgray;}
14+
code .lineOp {user-select: none; }
15+
/*
16+
table.code {border: 1px solid darkgrey; margin:1em; display: block; background-color: #eee; border-spacing: 0;}
17+
table.code .lineNum {user-select: none; background-color: lightgray;}
18+
table.code .add {color: green; font-weight: bolder;}
19+
table.code .remove {color: red; text-decoration: line-through;}
20+
*/
21+
@media print {
22+
@page {
23+
margin: 1em;
24+
}
25+
h1 {page-break-before: always;}
26+
h1:first-of-type {page-break-before: avoid;}
27+
28+
#navigation, #source { /* and (min-width: 500px) */
29+
display: none;
30+
}
31+
table {page-break-inside:auto;}
32+
tr {
33+
page-break-inside:avoid;
34+
page-break-after:auto;
35+
}
36+
}
37+
1438
</style>
1539

1640
</head>
@@ -97,22 +121,33 @@
97121

98122
const REGEX_DIFF_SEGMENT = /@@ [-+](?<line_start_old>\d+),(?<lines_changed_old>\d+) [-+](?<line_start_new>\d+),(?<lines_changed_new>\d+) @@\n(?<code>.*?)(?=$|\n@@)/sg
99123
function renderDiffSub(code_segment, start_line) {
124+
/*
125+
return h('table',{class:"code"},code_segment.split("\n").map(line=>h('tr',{},[
126+
h('td',{class:"lineNum"},pad_line_number(start_line++)),
127+
h('td',{class:"lineOp"},' '),
128+
h('td',{
129+
class:["line",line.startsWith("+")?"add":"",line.startsWith("-")?"remove":""].filter(x=>x).join(" ")
130+
},line),
131+
])))
132+
*/
133+
const lineOpClass = (line)=>[line.startsWith("+")?"add":"",line.startsWith("-")?"remove":""].filter(x=>x).join(" ")
100134
return h('code',{}, [
101135
h('div',{class:"lineNum"}, code_segment.split("\n").map(line=>
102-
h('span',{},`${pad_line_number(start_line++)} \n`),
136+
h('span',{},`${pad_line_number(line.startsWith("-")?'':start_line++)} \n`),
137+
)),
138+
h('div',{class:"lineOp"}, code_segment.split("\n").map(line=>
139+
h('span',{class:lineOpClass(line)},`${line.substring(0,1)}\n`),
103140
)),
104-
h('div',{class:"code" }, code_segment.split("\n").map(line=>
105-
h('span',{
106-
class:["line",line.startsWith("+")?"add":"",line.startsWith("-")?"remove":""].filter(x=>x).join(" ")
107-
}, line+"\n"),
141+
h('div',{class:"code"}, code_segment.split("\n").map(line=>
142+
h('span',{class: "line "+lineOpClass(line)}, `${line.substring(1)}\n`),
108143
))
109144
])
110145
}
111146
function renderDiff(diff) {
112147
if (!diff) {return}
113148
return [...diff.matchAll(REGEX_DIFF_SEGMENT)].map(match=>renderDiffSub(match.groups.code, match.groups.line_start_new))
114149
}
115-
function renderCodeFull(code) {return renderDiffSub(code,1)}
150+
function renderCodeFull(code) {return renderDiffSub(code.split('\n').map(line=>" "+line).join("\n"),1)}
116151

117152

118153
// Clipboard

0 commit comments

Comments
 (0)