-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpreston.js
120 lines (112 loc) · 3.42 KB
/
preston.js
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
function parsePreston(text){
var lines = text.split('\n')
, ret = []
, curr = null
, state = 'notes' // possible states: text, notes, code
lines.forEach(function(line){
if (state === 'code' || line.trim()){
var indent = line.match(/^[ \t]*/)[0]
if (state === 'notes' && !indent){
if (curr)
ret.push(curr)
if (line.match(/^<pre>.*<\/pre>[ \t]*$/)){
curr = {text: '', notes: '', code: line}
state = 'text'
}else if (line.match(/^<pre>/)){
curr = {text: '', notes: '', code: line + '\n'}
state = 'code'
}else{
curr = {text: line + '\n', notes: '', code: ''}
state = 'text'
}
}else if (state === 'notes' && indent){
curr.notes += line.substring(indent.length) + '\n'
}else if (state === 'text' && !indent){
curr.text += line + '\n'
}else if (state === 'text' && indent){
curr.notes += line.substring(indent.length) + '\n'
state = 'notes'
}else if (state === 'code'){
curr.code += line + '\n'
if (line.match(/<\/pre>[ \t]*$/))
state = 'text'
}
}
})
if (curr && (curr.text || curr.code))
ret.push(curr)
return ret
}
function markdown(text){
return (new (Showdown.converter)).makeHtml(text)
}
var index = -1, win, slides
function nextSlide(){
var slide = slides[++index]
if (!slide){
index--
return
}
showSlide(slide)
}
function prevSlide(){
var slide = slides[--index]
if (!slide){
index++
return
}
showSlide(slide)
}
function showSlide(slide){
var content = win.document.getElementById('content')
, localContent = document.getElementById('slide')
, notes = document.getElementById('notes')
slideText = slide.text || slide.code
content.innerHTML = localContent.innerHTML = markdown(slideText)
notes.innerHTML = markdown(slide.notes)
win.$('a').attr('target', '_blank')
win.fit()
}
function runSlides(slides){
$('#prevBtn').click(prevSlide)
$('#nextBtn').click(nextSlide)
index = -1
win = window.open('slide.html', 'slides',
'width=' + window.outerWidth + ',height=' +
window.outerHeight)
win.moveTo(0, 0)
setTimeout(function(){
win.nextSlide = nextSlide
win.prevSlide = prevSlide
nextSlide()
}, 500)
}
function resize(){
var height = $(window).height()
$('#layout').css({height: height + 'px'})
}
$(function(){
$(window).resize(resize)
resize()
$('#prevBtn, #nextBtn').hide()
$.ajax({
url: 'presentation.md',
dataType: 'text',
success: function(data){
$(document).keyup(function(e){
var code = e.keyCode
if (code == 39)
nextSlide()
if (code == 37)
prevSlide()
})
$('#startBtn').click(function(){
slides = parsePreston(data)
runSlides(slides)
$('.begin').removeClass('begin')
$('#info').html('The slides are in a separate window. Use ← and → to flip slides')
resize()
})
}
})
})