Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

# -*- coding: utf-8 -*- 

""" 

sphinxjp.themes.revealjs.directives 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 

 

:author: tell-k <ffk2005@gmail.com> 

:copyright: tell-k. All Rights Reserved. 

""" 

from docutils import nodes 

from docutils.parsers.rst import directives 

from docutils.parsers.rst.roles import set_classes 

from docutils.parsers.rst import Directive 

from . import compat 

 

__docformat__ = 'reStructuredText' 

 

 

class revealjs(nodes.General, nodes.Element): 

""" node for revealjs """ 

 

 

class rv_code(nodes.General, nodes.Element): 

""" node for revealjs code section """ 

 

 

class rv_small(nodes.General, nodes.Element): 

""" node for revealjs small text section """ 

 

 

class rv_note(nodes.General, nodes.Element): 

""" node for revealjs presentation note """ 

 

 

def heading(argument): 

""" directives choices for heading tag """ 

return directives.choice(argument, ('h1', 'h2', 'h3', 'h4', 'h5', 'h6')) 

 

 

class RevealjsDirective(Directive): 

""" Reveal.JS slide entry """ 

 

has_content = True 

required_arguments = 0 

optional_arguments = 100 

final_argument_whitespace = False 

 

option_spec = { 

'id': directives.unchanged, 

'class': directives.class_option, 

'noheading': directives.flag, 

'title-heading': heading, 

'subtitle': directives.unchanged, 

'subtitle-heading': directives.unchanged, 

'data-autoslide': directives.unchanged, 

'data-markdown': directives.unchanged, 

'data-transition': directives.unchanged, 

'data-transition-speed': directives.unchanged, 

'data-background': directives.unchanged, 

'data-background-repeat': directives.unchanged, 

'data-background-size': directives.unchanged, 

'data-background-transition': directives.unchanged, 

'data-state': directives.unchanged, 

'data-separator': directives.unchanged, 

'data-separator-vertical': directives.unchanged, 

'data-separator-notes': directives.unchanged, 

'data-charset': directives.unchanged, 

} 

 

node_class = revealjs 

 

def run(self): 

""" build revealjs node """ 

 

set_classes(self.options) 

 

text = '\n'.join(self.content) 

node = self.node_class(text, **self.options) 

 

self.add_name(node) 

 

if "data-markdown" not in self.options: 

self.state.nested_parse(self.content, self.content_offset, node) 

 

if self.arguments: 

node['title'] = " ".join(self.arguments) 

 

node['noheading'] = ('noheading' in self.options) 

 

options_list = ( 

'id', 

'title-heading', 

'subtitle-heading', 

'data-autoslide', 

'data-transition', 

'data-transition-speed', 

'data-background', 

'data-background-repeat', 

'data-background-size', 

'data-background-transition', 

'data-state', 

'data-markdown', 

'data-separator', 

'data-separator-vertical', 

'data-separator-notes', 

'data-charset', 

) 

for option in options_list: 

if option in self.options: 

node[option] = self.options.get(option) 

return [node] 

 

 

class RvSmallDirective(Directive): 

""" 

Create small text tag. 

""" 

has_content = True 

required_arguments = 0 

optional_arguments = 0 

final_argument_whitespace = False 

 

option_spec = { 

'class': directives.class_option, 

} 

node_class = rv_small 

 

def run(self): 

""" build rv_small node """ 

 

set_classes(self.options) 

self.assert_has_content() 

text = '\n'.join(self.content) 

node = self.node_class(text, **self.options) 

self.add_name(node) 

self.state.nested_parse(self.content, self.content_offset, node) 

return [node] 

 

 

class RvNoteDirective(Directive): 

""" 

Directive for a notes tag. 

""" 

has_content = True 

required_arguments = 0 

optional_arguments = 0 

final_argument_whitespace = False 

 

option_spec = { 

'class': directives.class_option, 

} 

node_class = rv_note 

 

def run(self): 

""" build rv_note node """ 

set_classes(self.options) 

self.assert_has_content() 

text = '\n'.join(self.content) 

node = self.node_class(text, **self.options) 

self.add_name(node) 

self.state.nested_parse(self.content, self.content_offset, node) 

return [node] 

 

 

class RvCodeDirective(Directive): 

""" 

Directive for a code block with highlight.js 

""" 

 

has_content = True 

required_arguments = 0 

optional_arguments = 0 

final_argument_whitespace = False 

option_spec = { 

'id': directives.unchanged, 

'class': directives.class_option, 

} 

node_class = rv_code 

 

def run(self): 

""" build rv_code node """ 

set_classes(self.options) 

self.assert_has_content() 

node = self.node_class('\n'.join(self.content), **self.options) 

return [node] 

 

 

def visit_revealjs(self, node): 

""" build start tag for revealjs """ 

section_attr = {} 

markdown_headings = {"h1": "#", "h2": "##", "h3": "###", 

"h4": "####", "h5": "#####", "h6": "######"} 

 

if node.get("id"): 

section_attr.update({"ids": [node.get("id")]}) 

 

attr_list = ( 

'data-autoslide', 

'data-transition', 

'data-transition-speed', 

'data-background', 

'data-background-repeat', 

'data-background-size', 

'data-background-transition', 

'data-state', 

'data-markdown', 

'data-separator', 

'data-separator-vertical', 

'data-separator-notes', 

'data-charset', 

) 

for attr in attr_list: 

if node.get(attr) is not None: 

section_attr.update({attr: node.get(attr)}) 

 

title = None 

if node.get("title") and not node.get('noheading'): 

title = node.get("title") 

 

title_heading = node.get('title-heading', 'h2') 

subtitle = node.get("subtitle") 

subtitle_heading = node.get('subtitle-heading', 'h3') 

if node.get("data-markdown") is not None: 

 

title_base = compat.text("%(heading)s %(title)s \n") 

title_text = None 

if title: 

title_text = title_base % dict( 

heading=markdown_headings.get(title_heading), 

title=title 

) 

 

subtitle_text = None 

if subtitle: 

subtitle_text = title_base % dict( 

heading=markdown_headings.get(subtitle_heading), 

title=subtitle 

) 

else: 

title_base = compat.text("<%(heading)s>%(title)s</%(heading)s>\n") 

title_text = None 

if title: 

title_text = title_base % dict( 

title=title, 

heading=title_heading) 

 

subtitle_text = None 

if subtitle: 

subtitle_text = title_base % dict( 

title=subtitle, 

heading=subtitle_heading) 

 

if node.get("data-markdown") is not None: 

self.body.append(self.starttag(node, 'section', **section_attr)) 

if node.get("data-markdown") == compat.text(""): 

self.body.append("<script type='text/template'>\n") 

if title_text: 

self.body.append(title_text) 

if subtitle_text: 

self.body.append(subtitle_text) 

self.body.append(node.rawsource) 

self.body.append("</script>\n") 

else: 

self.body.append(self.starttag(node, 'section', **section_attr)) 

if title_text: 

self.body.append(title_text) 

if subtitle_text: 

self.body.append(subtitle_text) 

self.set_first_last(node) 

 

 

def depart_revealjs(self, node=None): 

""" build end tag for revealjs """ 

self.body.append('</section>\n') 

 

 

def visit_rv_code(self, node): 

""" build start tag for rv_code """ 

 

self.body.append(self.starttag(node, 'pre')) 

self.body.append("<code data-trim contenteditable>") 

self.body.append(compat.escape_html(node.rawsource)) 

 

 

def depart_rv_code(self, node=None): 

""" build end tag for rv_code """ 

 

self.body.append("</code>") 

self.body.append("</pre>\n") 

 

 

def visit_rv_small(self, node): 

""" build start tag for rv_small """ 

self.body.append(self.starttag(node, 'small')) 

self.set_first_last(node) 

 

 

def depart_rv_small(self, node=None): 

""" build end tag for rv_small""" 

self.body.append("</small>\n") 

 

 

def visit_rv_note(self, node): 

""" build start tag for rv_note """ 

self.body.append(self.starttag(node, 'aside', **{'class': 'notes'})) 

self.set_first_last(node) 

 

 

def depart_rv_note(self, node=None): 

""" build end tag for rv_note """ 

self.body.append("</aside>\n") 

 

 

def setup(app): 

"""Initialize """ 

app.add_node(revealjs, html=(visit_revealjs, depart_revealjs)) 

app.add_node(rv_code, html=(visit_rv_code, depart_rv_code)) 

app.add_node(rv_note, html=(visit_rv_note, depart_rv_note)) 

app.add_node(rv_small, html=(visit_rv_small, depart_rv_small)) 

app.add_directive('revealjs', RevealjsDirective) 

app.add_directive('rv_code', RvCodeDirective) 

app.add_directive('rv_note', RvNoteDirective) 

app.add_directive('rv_small', RvSmallDirective) 

return app