|
7 | 7 | <style>
|
8 | 8 | .hidden {display: none;}
|
9 | 9 | code {border: 1px solid darkgrey; margin:1em; display: block; background-color: #eee;}
|
| 10 | + code div {display: inline-block;} |
10 | 11 | code .add {color: green; font-weight: bolder;}
|
11 | 12 | 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 | + |
14 | 38 | </style>
|
15 | 39 |
|
16 | 40 | </head>
|
|
97 | 121 |
|
98 | 122 | const REGEX_DIFF_SEGMENT = /@@ [-+](?<line_start_old>\d+),(?<lines_changed_old>\d+) [-+](?<line_start_new>\d+),(?<lines_changed_new>\d+) @@\n(?<code>.*?)(?=$|\n@@)/sg
|
99 | 123 | 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(" ") |
100 | 134 | return h('code',{}, [
|
101 | 135 | 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`), |
103 | 140 | )),
|
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`), |
108 | 143 | ))
|
109 | 144 | ])
|
110 | 145 | }
|
111 | 146 | function renderDiff(diff) {
|
112 | 147 | if (!diff) {return}
|
113 | 148 | return [...diff.matchAll(REGEX_DIFF_SEGMENT)].map(match=>renderDiffSub(match.groups.code, match.groups.line_start_new))
|
114 | 149 | }
|
115 |
| -function renderCodeFull(code) {return renderDiffSub(code,1)} |
| 150 | +function renderCodeFull(code) {return renderDiffSub(code.split('\n').map(line=>" "+line).join("\n"),1)} |
116 | 151 |
|
117 | 152 |
|
118 | 153 | // Clipboard
|
|
0 commit comments