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

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

437

438

439

440

441

442

443

444

445

446

447

448

449

450

451

452

453

454

455

456

457

458

459

460

461

462

463

464

465

466

467

468

469

470

471

472

473

474

475

476

477

478

479

480

481

482

483

484

485

486

487

488

489

490

491

492

493

494

495

496

497

498

499

500

501

502

503

#! /usr/bin/env python3 

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

 

#       Copyright 2013, Milan Boers 

#       Copyright 2013, 2017, Marten de Vries 

# 

#       This file is part of OpenTeacher. 

# 

#       OpenTeacher is free software: you can redistribute it and/or modify 

#       it under the terms of the GNU General Public License as published by 

#       the Free Software Foundation, either version 3 of the License, or 

#       (at your option) any later version. 

# 

#       OpenTeacher is distributed in the hope that it will be useful, 

#       but WITHOUT ANY WARRANTY; without even the implied warranty of 

#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 

#       GNU General Public License for more details. 

# 

#       You should have received a copy of the GNU General Public License 

#       along with OpenTeacher.  If not, see <http://www.gnu.org/licenses/>. 

 

import sys 

import os 

import posixpath 

import shutil 

import tempfile 

import atexit 

import json 

import glob 

 

DOWNLOAD_LINKS = { 

        "windows": "https://sourceforge.net/projects/openteacher/files/openteacher/3.2/openteacher-3.2-windows-setup.msi/download", 

        "windows-portable": "https://sourceforge.net/projects/openteacher/files/openteacher/3.2/openteacher-3.2-windows-portable.zip/download", 

        "osx": "https://sourceforge.net/projects/openteacher/files/openteacher/3.2/openteacher_3.2_mac.dmg/download", 

        "ubuntu": "https://sourceforge.net/projects/openteacher/files/openteacher/3.3/openteacher_3.3.deb/download", 

        "fedora": "https://sourceforge.net/projects/openteacher/files/openteacher/3.3/openteacher_3.3_fedora.rpm/download", 

        "opensuse": "https://sourceforge.net/projects/openteacher/files/openteacher/3.3/openteacher_3.3_opensuse.rpm/download", 

        "arch": "https://sourceforge.net/projects/openteacher/files/openteacher/3.3/openteacher_3.3_arch.pkg.tar.xz/download", 

        "linux": "https://sourceforge.net/projects/openteacher/files/openteacher/3.3/openteacher_3.3_linux.tar.gz/download", 

        "source": "https://sourceforge.net/projects/openteacher/files/openteacher/3.3/openteacher_3.3_linux.tar.gz/download", 

} 

 

#image generators 

class ButtonImagesGenerator: 

        def __init__(self, hue, *args, **kwargs): 

                super().__init__(*args, **kwargs) 

 

                self._hue = hue 

 

        def generateButtons(self, logoAndOutputNames): 

                for logoPath, outputBasename in logoAndOutputNames: 

                        self._generateHoveredAndNormalButtons(logoPath, outputBasename) 

 

        def _generateHoveredAndNormalButtons(self, logoPath, basename): 

                gradientTopColor = QtGui.QColor.fromHsv(self._hue, 46, 246) 

                gradientBottomColor = QtGui.QColor.fromHsv(self._hue, 58, 229) 

                self._generateDownloadButton(gradientTopColor, gradientBottomColor, logoPath, basename + ".png") 

 

                hoveredGradientTopColor = QtGui.QColor.fromHsv(self._hue, 47, 251) 

                hoveredGradientBottomColor = QtGui.QColor.fromHsv(self._hue, 57, 236) 

                self._generateDownloadButton(hoveredGradientTopColor, hoveredGradientBottomColor, logoPath, basename + "-h.png") 

 

        def _generateDownloadButton(self, gradientTopColor, gradientBottomColor, logoPath, path): 

                width = 382 

                height = 66 

                radius = 9 

                xMargin = 9 

                yMargin = 10 

                logoHeight = 44 

 

                borderColor = QtGui.QColor.fromHsv(-1, 0, 146) 

 

                gradient = QtGui.QLinearGradient(0, 0, 0, height) 

                gradient.setColorAt(0, gradientTopColor) 

                gradient.setColorAt(1, gradientBottomColor) 

 

                logo = QtGui.QImage(logoPath).scaledToHeight(logoHeight, QtCore.Qt.SmoothTransformation) 

 

                img = QtGui.QImage(width, height, QtGui.QImage.Format_ARGB32_Premultiplied) 

                img.fill(QtCore.Qt.transparent) 

                painter = QtGui.QPainter(img) 

 

                painter.setPen(borderColor) 

                painter.setBrush(gradient) 

                painter.drawRoundedRect(0, 0, width -1, height -1, radius, radius) 

                painter.drawImage(xMargin, yMargin, logo) 

 

                painter.end() 

                img.save(path) 

 

class LightImage: 

        def __init__(self, hue, *args, **kwargs): 

                super().__init__(*args, **kwargs) 

 

                self._hue = hue 

 

        def save(self, path): 

                """Generates a half circle filled with a gradient to be used to 

                   show which menu item the mouse is at/which page is active. 

 

                """ 

                width = 26 

                height = 13 

                color = QtGui.QColor.fromHsv(self._hue, 59, 240, 255) 

 

                img = QtGui.QImage(width, height, QtGui.QImage.Format_ARGB32_Premultiplied) 

                img.fill(QtCore.Qt.transparent) 

                painter = QtGui.QPainter(img) 

 

                gradient = QtGui.QRadialGradient(width / 2, 0, height / 4 * 3) 

                gradient.setColorAt(0, color) 

                gradient.setColorAt(0.4, color) 

                gradient.setColorAt(1, QtCore.Qt.transparent) 

 

                painter.setPen(QtCore.Qt.NoPen) 

                painter.setBrush(gradient) 

 

                painter.drawRect(0, 0, width, height) 

 

                painter.end() 

                img.save(path) 

 

class BackgroundImagesGenerator: 

        def __init__(self, hue, generateBodyBackgroundImage, *args, **kwargs): 

                super().__init__(*args, **kwargs) 

 

                self._hue = hue 

                self._generateBodyBackgroundImage = generateBodyBackgroundImage 

 

        def generateBackground(self, path): 

                """Generates the background file: a small bug high file with 

                   nothing but a gradient inside. 

 

                """ 

                width = 1 

                height = 1000 

                startColor = QtGui.QColor.fromHsv(self._hue, 43, 250) 

                endColor = QtGui.QColor.fromHsv(self._hue, 21, 227) 

 

                img = QtGui.QImage(width, height, QtGui.QImage.Format_ARGB32_Premultiplied) 

                img.fill(QtCore.Qt.transparent) 

 

                gradient = QtGui.QLinearGradient(0, 0, 0, height) 

                gradient.setColorAt(0, startColor) 

                gradient.setColorAt(1, endColor) 

 

                painter = QtGui.QPainter(img) 

                painter.setBrush(gradient) 

                painter.setPen(QtCore.Qt.NoPen) 

                painter.drawRect(0, 0, width, height) 

                painter.end() 

                img.save(path) 

 

        def generateBodyBackground(self, path): 

                img = self._generateBodyBackgroundImage() 

 

                #and save it. 

                img.save(path) 

 

#style sheet 

class StyleSheet: 

        def __init__(self, hue, lineColor, template, *args, **kwargs): 

                super().__init__(*args, **kwargs) 

 

                self._hue = hue 

                self._lineColor = lineColor 

                self._template = template 

 

        def save(self, path): 

                """Generates the style sheet of OpenTeacher using ``self._hue`` 

                   (which comes from metadata) for the colors. 

 

                """ 

                t = pyratemp.Template(filename=self._template) 

                with open(path, "w", encoding='UTF-8') as f: 

                        f.write(t(**{ 

                                "aLinkColor": QtGui.QColor.fromHsv(self._hue, 63, 101).name(), 

                                "aHoverColor": QtGui.QColor.fromHsv(self._hue, 66, 159).name(), 

                                "bodyBackgroundColor": QtGui.QColor.fromHsv(self._hue, 30, 228).name(), 

                                "bodyTextColor": QtGui.QColor.fromHsv(self._hue, 64, 64).name(), 

                                "hrColor": self._lineColor, 

                                "downloadTableBorderColor": self._lineColor, 

                                "downloadRowBackgroundColor": QtGui.QColor.fromHsv(self._hue, 27, 234).name(), 

                                "downloadRowBorderColor": self._lineColor, 

                                "codeBlockBackgroundColor": QtGui.QColor.fromHsv(self._hue, 21, 240).name(), 

                        })) 

 

class WebsiteGeneratorModule: 

        def __init__(self, moduleManager, *args, **kwargs): 

                super().__init__(*args, **kwargs) 

                self._mm = moduleManager 

 

                self.type = "websiteGenerator" 

 

                self.requires = ( 

                        self._mm.mods(type="execute"), 

                        self._mm.mods(type="ui"), 

                        self._mm.mods(type="metadata"), 

                        self._mm.mods(type="userDocumentation"), 

                        self._mm.mods(type="userDocumentationWrapper"), 

                        self._mm.mods(type="backgroundImageGenerator"), 

                        self._mm.mods(type="friendlyTranslationNames"), 

                ) 

                self.uses = ( 

                        self._mm.mods(type="translator"), 

                ) 

 

                self.priorities = { 

                        "generate-website": 0, 

                        "default": -1, 

                } 

                self.devMod = True 

 

                self._templatesDir = self._mm.resourcePath("templates") 

                self._docsTemplatesDir = self._mm.resourcePath("docsTemplates") 

 

                templatesFiles = [os.path.join("templates", f) for f in os.listdir(self._templatesDir)] 

                docTemplatesFiles = [os.path.join("docsTemplates", f) for f in os.listdir(self._docsTemplatesDir)] 

                self.filesWithTranslations = templatesFiles + docTemplatesFiles 

 

                self._tempPaths = [] 

                atexit.register(self._removeTempDirs) 

 

        def _removeTempDirs(self): 

                for path in self._tempPaths: 

                        shutil.rmtree(path) 

 

        def generateWebsite(self): 

                """Generates the complete OT website into a directory specified 

                   as the first command line argument. 

 

                """ 

                self._path = self._makeOutputDir() 

                if not self._path: 

                        return 

 

                self._copyResources() 

                self._generateResources() 

                self._generateHtml() 

 

                print("Writing OpenTeacher website to '%s' is now done." % self._path) 

 

        def _makeOutputDir(self): 

                """Gets the output directory name and creates it. Asks if 

                   overwriting it is allowed in case that's needed. Returns True 

                   on success, otherwise False. 

 

                """ 

                #get path to save to 

                try: 

                        path = sys.argv[1] 

                except IndexError: 

                        print("Please specify a path to save the website to. (e.g. -p generate-website website-debug)", file=sys.stderr) 

                        return 

                #ask if overwrite 

                if os.path.exists(path): 

                        confirm = input("There is already a directory at '%s'. Do you want to remove it and continue (y/n). " % path) 

                        if confirm != "y": 

                                return 

                        shutil.rmtree(path) 

 

                os.mkdir(path) 

                return path 

 

        @property 

        def _userDocMod(self): 

                return self._modules.default("active", type="userDocumentation") 

 

        @property 

        def _userDocWrapperMod(self): 

                return self._modules.default("active", type="userDocumentationWrapper") 

 

        def _copyResources(self): 

                """Copies all static website resources into place.""" 

 

                copyTree = lambda name: shutil.copytree( 

                        self._mm.resourcePath(name), 

                        os.path.join(self._path, name) 

                ) 

                copy = lambda name: shutil.copy( 

                        self._mm.resourcePath(name), 

                        os.path.join(self._path, name) 

                ) 

                # Copy images, scripts etc. 

                copyTree("images") 

                copyTree("scripts") 

                copyTree("inAppDocs") 

                copyTree("files") 

                copy("index.php") 

                copy("documentation.html") 

 

                #Copy user documentation's static files 

                shutil.copytree( 

                        self._userDocMod.resourcesPath, 

                        os.path.join(self._path, "images/docs/3"), 

                ) 

 

        def _generateResources(self): 

                """Generates a few resources.""" 

 

                toPath = lambda part: os.path.join(self._path, part) 

                fromPath = lambda part: self._mm.resourcePath(os.path.join("images/oslogos/", part)) 

 

                #stylesheet 

                style = StyleSheet(self._hue, self._lineColor, template=self._mm.resourcePath("style.css")) 

                style.save(toPath("style.css")) 

                #images 

                bgGenerator = BackgroundImagesGenerator(self._hue, self._modules.default("active", type="backgroundImageGenerator").generate) 

                bgGenerator.generateBackground(toPath("images/bg.png")) 

                bgGenerator.generateBodyBackground(toPath("images/body.png")) 

                LightImage(self._hue).save(toPath("images/light.png")) 

                #button backgrounds (which include logos) 

                os.mkdir(toPath('images/downloadbuttons')) 

                ButtonImagesGenerator(self._hue).generateButtons([ 

                        (fromPath("fedoralogo.png"), toPath("images/downloadbuttons/fedora-button")), 

                        (fromPath("tuxlogo.png"), toPath("images/downloadbuttons/linux-button")), 

                        (fromPath("osxlogo.png"), toPath("images/downloadbuttons/osx-button")), 

                        (fromPath("ubulogo.png"), toPath("images/downloadbuttons/ubuntu-button")), 

                        (fromPath("winlogo.png"), toPath("images/downloadbuttons/windows-button")), 

                ]) 

                #icon 

                shutil.copy(self._iconPath, toPath("images/openteacher-icon.png")) 

 

        def _generateHtml(self): 

                """Generates all html files: first for US English and then for 

                   all the available translations. If that's done, it generates 

                   the in app documentation pages (that are in a separate 

                   directory). 

 

                """ 

                # The default (US English) (str as translate function) 

                self._tr = str 

                self._langCode = "en" 

                self._generatePages() 

 

                try: 

                        translator = self._modules.default("active", type="translator") 

                except IndexError: 

                        pass 

                else: 

                        # All the other languages 

                        for moname in os.listdir(self._mm.resourcePath('translations')): 

                                if not moname.endswith('.mo'): 

                                        continue 

                                self._langCode = os.path.splitext(moname)[0] 

 

                                # Set translation function 

                                _, ngettext = translator.gettextFunctions(self._mm.resourcePath("translations"), language=self._langCode) 

                                self._tr = _ 

 

                                # Generate 

                                self._generatePages() 

 

                # Generate inAppDocumentation pages. 

                for lang in list(self._userDocMod.availableTranslations) + ["en"]: 

                        destination = os.path.join(self._path, "inAppDocs", lang + ".html") 

                        html = self._getWrappedUserDocumentation(lang) 

                        with open(destination, "w", encoding='UTF-8') as f: 

                                f.write(html) 

 

        def _generatePages(self): 

                """Generates all pages in a certain language in a subdirectory 

                   of the main output path. First it generates the normal pages, 

                   then the documentation ones. 

 

                """ 

                self._langDir = os.path.join(self._path, self._langCode) 

                os.mkdir(self._langDir) 

 

                templates = ["about.html", "news.html", "download.html", "documentation.html", "index.html", "contribute.html"] 

                for pageName in templates: 

                        self._generatePage(pageName) 

 

                docsDir = os.path.join(self._langDir, "documentation") 

                os.mkdir(docsDir) 

                documentationTemplatePaths = [ 

                        os.path.join(self._docsTemplatesDir, f) 

                        for f in os.listdir(self._docsTemplatesDir) 

                ] 

                documentationTemplatePaths.append(self._getUsingOpenTeacher3Path()) 

                for filename in documentationTemplatePaths: 

                        self._generateDocumentationPage(filename) 

 

        def _getUsingOpenTeacher3Path(self): 

                path = tempfile.mkdtemp() 

                #make sure it's cleaned up... 

                self._tempPaths.append(path) 

 

                filePath = os.path.join(path, "using-openteacher-3.html") 

                html = self._userDocMod.getHtml("../images/docs/3", self._langCode) 

                with open(filePath, "w", encoding="UTF-8") as f: 

                        f.write(html) 

                return filePath 

 

        def _getWrappedUserDocumentation(self, lang): 

                html = self._userDocMod.getHtml("../images/docs/3", lang) 

                return self._userDocWrapperMod.wrap(html) 

 

        def _generateDocumentationPage(self, templatePath): 

                """Combines the documentation text, with the stuff shared 

                   between all documentation pages, and wraps the result in a 

                   page. 

 

                """ 

                #the documentation text 

                pageName = "documentation/" + os.path.basename(templatePath) 

                docContent = self._evaluateTemplate(templatePath, pageName) 

 

                #the documentation wrapper 

                templatePath = os.path.join(self._templatesDir, "docpage.html") 

                content = self._evaluateTemplate(templatePath, pageName, docContent=docContent) 

 

                self._writePage(pageName, content) 

 

        def _generatePage(self, pageName): 

                """Gets the content of the page, and writes it into a page.""" 

 

                filename = os.path.join(self._templatesDir, pageName) 

                content = self._evaluateTemplate(filename, pageName, downloadLinks=DOWNLOAD_LINKS, newsFeedUrl=self._newsFeedUrl, version=self._version) 

 

                self._writePage(pageName, content) 

 

        def _writePage(self, pageName, content): 

                """Wraps content into a page template and writes the result to 

                   disk 

 

                """ 

                #write content file (used by ajax) 

                with open(os.path.join(self._langDir, os.path.splitext(pageName)[0] + ".contentonly.html"), "w", encoding='UTF-8') as f: 

                        f.write(content) 

                page = self._wrapContent(pageName, content) 

 

                #write html file (used on first page load) 

                with open(os.path.join(self._langDir, pageName), "w", encoding="UTF-8") as f: 

                        f.write(page) 

 

        @property 

        def _languages(self, cache={}): 

                if not cache: 

                        translationNames = self._modules.default("active", type="friendlyTranslationNames").friendlyNames 

 

                        cache["en"] = translationNames["C"] 

                        for mofile in glob.glob(self._mm.resourcePath("translations/*.mo")): 

                                name = os.path.splitext(os.path.basename(mofile))[0] 

                                cache[name] = translationNames.get(name, name) 

                return cache 

 

        def _wrapContent(self, pageName, content): 

                """Wraps content into a page template""" 

 

                filename = os.path.join(self._templatesDir, "base.html") 

                pageName = os.path.splitext(pageName)[0] 

                return self._evaluateTemplate(filename, pageName, 

                        pageName=pageName, 

                        currentLanguage=self._langCode, 

                        languages=self._languages, 

                        content=content, 

                        downloadLinksJson=json.dumps(DOWNLOAD_LINKS) 

                ) 

 

        def _evaluateTemplate(self, templatePath, thisPage, **kwargs): 

                class EvalPseudoSandbox(pyratemp.EvalPseudoSandbox): 

                        def __init__(self2, *args, **kwargs): 

                                pyratemp.EvalPseudoSandbox.__init__(self2, *args, **kwargs) 

                                self2.register("tr", self._tr) 

                                currentDir = os.path.dirname(thisPage) 

                                self2.register("url", lambda name: posixpath.relpath(name, currentDir)) 

 

                t = pyratemp.Template(filename=templatePath, eval_class=EvalPseudoSandbox) 

                return t(**kwargs) 

 

        def enable(self): 

                global pyratemp, QtCore, QtGui 

                try: 

                        import pyratemp 

                        from PyQt5 import QtCore, QtGui 

                except ImportError: 

                        sys.stderr.write("For this developer module to work, you need to have pyratemp & PyQt5 installed.\n") 

 

                self._modules = next(iter(self._mm.mods(type="modules"))) 

                self._modules.default(type="execute").startRunning.handle(self.generateWebsite) 

 

                metadata = self._modules.default("active", type="metadata").metadata 

                self._iconPath = metadata["iconPath"] 

                self._hue = metadata["mainColorHue"] 

                self._newsFeedUrl = metadata["newsFeedUrl"] 

                self._version = metadata["version"] 

                self._lineColor = self._modules.default("active", type="backgroundImageGenerator").lineColor 

 

                self.active = True 

 

        def disable(self): 

                self.active = False 

 

                del self._modules 

                del self._iconPath 

                del self._hue 

                del self._newsFeedUrl 

                del self._version 

                del self._lineColor 

 

def init(moduleManager): 

        return WebsiteGeneratorModule(moduleManager)