-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransformer.html
122 lines (106 loc) · 3.31 KB
/
transformer.html
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
116
117
118
119
120
121
122
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Microdata Template: Example: Transformers</title>
<link rel="icon" href="data:,">
<script src="../../lib/microdata-template.js" crossorigin="anonymous"></script>
<style>
body {
font-family: sans-serif;
}
section {
width: 36%;
margin: 3em auto;
}
blockquote {
padding: 8px;
border-bottom: 1px dotted orangered;
color: #0776A2;
}
blockquote:hover {
background-color: #eeeedd;
}
blockquote h5 {
margin: 0;
}
blockquote p {
margin: 1em;
font-size: larger;
line-height: 1.3;
color: black;
}
blockquote time {
display: inline-block;
width: 100%;
text-align: right;
font-size: smaller;
color: lightsteelblue;
}
a {
color: #0776A2;
border-bottom: 1px solid lightsteelblue;
text-decoration: none;
}
a:visited {
color: lightsteelblue;
}
blockquote:hover a {
color: #0776A2;
}
</style>
</head>
<body>
<main>
<article>
<section>
<blockquote itemscope itemid="{{ id }}" itemtype="https://schema.org/UserTweets" hidden>
<h5 itemprop="tweet"><a href="{{ combineString:('http://twitter.com/', user.screen_name, '/status/', id) }}">{{ concat:combineString:("@", user[screen_name], " tweeted from ", user.location) }}:</a></h5>
<p itemprop="text">{{ tryThis|tryThat|text|"Sorry, that tweet wasn't found!" }}</p>
<time itemprop="created_at" datetime="{{ parseDateToTimeValue:created_at }}">{{ formatDate:created_at }}</time>
</blockquote>
</section>
</article>
</main>
<script>
document.addEventListener("DOMContentLoaded", function() {
// This code will load an adjacent data file and use it to draw a series of tweets.
// Also, a custom transformer is employed for special formatting of date values.
// Run this in a browser, and be sure to view the rendered source!
var templater = window.MicrodataTemplate.init();
var templateElement = document.getElementsByTagName("BLOCKQUOTE")[0];
var ajax = function(endpoint, options, callback) {
var request = new XMLHttpRequest();
request.open("GET", endpoint, true);
request.onload = function() {
if (request.status >= 200 && request.status < 400) {
// Success!
if (!!callback && callback instanceof Function) {
callback(JSON.parse(request.responseText)||{});
}
} else {
// Error!
alert("Sorry, but this example was unable to fetch data. (see console for details)");
console.error(request.status, endpoint);
}
};
request.onerror = function() {
// There was a connection error of some sort
alert("Sorry, but this example must run in a hosted environment attached to the internet.");
};
request.send();
};
ajax("../data/transformer.json", null, function(data) {
templater.setTransformer("formatDate", function(value, index) {
var time = Date.parse(value);
var date = new Date(isNaN(time) ? isNaN(value) ? Date.now() : value : time);
return date.toLocaleString();
});
if (!!data) {
templater.render(templateElement, data);
}
});
});
</script>
</body>
</html>