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

from builtins import str 

from builtins import object 

# Copyright 2011-2012 Luc Saffre 

# License: BSD (see file COPYING for details) 

 

import cgi 

 

from django.utils.functional import Promise 

from django.utils.encoding import force_text 

 

from lino.utils.restify import restify 

 

 

CONVERTERS = [] 

 

 

def py2html(v): 

    #~ logger.debug("py2js(%r)",v) 

    for cv in CONVERTERS: 

        v = cv(v) 

    if isinstance(v, Promise): 

        v = force_text(v) 

    if isinstance(v, HTML): 

        return v.__html__() 

    if callable(v): 

        return "\n".join([ln for ln in v()]) 

    return cgi.escape(str(v)) 

 

 

class HTML(object): 

 

    def __init__(self, html_code): 

        self.html_code = html_code 

 

    def __html__(self): 

        return self.html_code 

 

 

def LIST(tag, items): 

    s = '\n'.join(['<li>%s</li>' % py2html(i) for i in items]) 

    return "<%s>%s</%s>" % (tag, s, tag) 

 

 

def UL(items): 

    return LIST('UL', items) 

 

 

def OL(items): 

    return LIST('OL', items) 

 

 

def H(level, text): 

    return "<h%d>%s</h%d>" % (level, cgi.escape(text), level) 

 

 

def DIV(content): 

    return "<DIV>%s</DIV>" % py2html(content) 

 

 

def TH(content): 

    return "<th>%s</th>" % py2html(content) 

 

 

def TD(content): 

    return "<TD>%s</TD>" % py2html(content) 

 

 

class TABLE(object): 

 

    """ 

    Renders as a simple table. 

     

    """ 

 

    def __init__(self, headers, show_headers=True): 

        self.headers = headers 

        self.show_headers = show_headers 

 

    def html_lines(self, rows): 

        yield "<TABLE>" 

        if self.show_headers: 

            s = ''.join([TH(HTML(h)) for h in self.headers]) 

            yield "<TR>%s</TR>" % s 

 

        for row in rows: 

            s = ''.join([TD(HTML(cell)) for cell in row]) 

            yield "<TR>%s</TR>" % s 

        yield "</TABLE>"