-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
robustify.go
115 lines (88 loc) · 3.34 KB
/
robustify.go
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
package httpreserve
import (
"fmt"
"regexp"
"strings"
"time"
)
// formatRobustDate returns a Robust formatted date for a given value.
func formatRobustDate(inputDate string) string {
const fourteenDigitDateFormat = "20060102150405"
outputDate, _ := time.Parse(fourteenDigitDateFormat, inputDate)
return fmt.Sprintf("%s", outputDate.Format("2006-01-02"))
}
// getRobustDates returns a simplified date format according to the
// RobustLinks specification.
//
// TODO: Some of this code is repeated from getISODates. We should be
// able to grab the dates early and then process without having to do
// the regext work or checking twice.
func getRobustDates(earliest string, latest string) (string, string) {
// Compile a regex to match just 14-digit numeric values.
regexPattern, _ := regexp.Compile("\\d{14}")
datetime := regexPattern.FindString(earliest)
if datetime == "" {
return "", ""
}
earliest = formatRobustDate(datetime)
// Latest date is implicit if there is an earliest.
latest = formatRobustDate(regexPattern.FindString(latest))
return earliest, latest
}
// makeReplacements will make the neccesary string replacements in our
// robust links.
func makeReplacements(html string, origin string, ia string, date string) string {
replaceDate := "{{ date }}"
replaceOrigin := "{{ origin }}"
replaceIA := "{{ ia }}"
html = strings.Replace(html, replaceDate, date, 1)
html = strings.Replace(html, replaceOrigin, origin, 1)
html = strings.Replace(html, replaceIA, ia, 1)
return html
}
// getRobust will return earliest and latest Robust links for the given
// resource, which looks something like follows:
//
// /*
// "RobustLinkEarliest": "
// <a href=\"http://web.archive.org/web/20020120142510/http://example.com/\"
// data-originalurl=\"http://example.com/\"
// data-versiondate=\"2002-01-20\">
// HTTPreserve Robust Link - simply replace this text!</a>",
// */
//
// NB. HTML fragment validator: https://appdevtools.com/html-validator
func getRobust(origin string, earliest string, latest string) (string, string) {
earliestDate, latestDate := getRobustDates(earliest, latest)
if earliestDate == "" {
return "", ""
}
early := "<a href='{{ ia }}'" +
"\x20data-originalurl='{{ origin }}'" +
"\x20data-versiondate='{{ date }}'>" +
"HTTPreserve Robust Link - simply replace this text!!</a>"
late := "<a href='{{ ia }}'" +
"\x20data-originalurl='{{ origin }}'" +
"\x20data-versiondate='{{ date }}'>" +
"HTTPreserve Robust Link - simply replace this text!!</a>"
early = makeReplacements(early, origin, earliest, earliestDate)
late = makeReplacements(late, origin, latest, latestDate)
return early, late
}
func getRobustEncoded(origin string, earliest string, latest string) (string, string) {
earliestDate, latestDate := getRobustDates(earliest, latest)
if earliestDate == "" {
return "", ""
}
early := "<a href='{{ ia }}'" +
"\x20data-originalurl='{{ origin }}'" +
"\x20data-versiondate='{{ date }}'>" +
"HTTPreserve Robust Link - simply replace this text!!</a>"
late := "<a href='{{ ia }}'" +
"\x20data-originalurl='{{ origin }}'" +
"\x20data-versiondate='{{ date }}'>" +
"HTTPreserve Robust Link - simply replace this text!!</a>"
early = makeReplacements(early, origin, earliest, earliestDate)
late = makeReplacements(late, origin, latest, latestDate)
return early, late
}