File: html_converter.py

package info (click to toggle)
gnome-blog 0.8-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,172 kB
  • ctags: 144
  • sloc: sh: 3,172; python: 1,144; makefile: 131; xml: 8
file content (85 lines) | stat: -rw-r--r-- 2,081 bytes parent folder | download
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
import gtk

def getHTML(buffer):
    global para_tag
    
    next_iter = buffer.get_start_iter()

    open_tags = [ para_tag ]
    html = "<p>" + _getTagsHTML(next_iter, open_tags)

    while not next_iter.is_end():
        iter = next_iter
        next_iter = _getNextIter(iter)

        new_text = buffer.get_text(iter, next_iter, gtk.TRUE)
        html = html + new_text + _getTagsHTML(next_iter, open_tags)

    return html

  
def _getNextIter(iter):
    nextTagIter  = iter.copy()
    endLineIter  = iter.copy()

    tagFound = nextTagIter.forward_to_tag_toggle(None)
    endLineIter.forward_to_line_end()

    position = nextTagIter.compare(endLineIter)

    if tagFound and position == -1:
        print ("Iter is for tag")
        next_iter = nextTagIter
    else:
        print ("Iter is for endline")
        next_iter = endLineIter

    return next_iter

def _getTags(iter):
    global para_tag
    
    turnontags  = iter.get_toggled_tags(gtk.TRUE)
    turnofftags = iter.get_toggled_tags(gtk.FALSE)

    if iter.is_end():
        # Iter is at the end of the buffer
        turnofftags.append(para_tag)
    elif iter.ends_line():
        turnofftags.append(para_tag)
        turnontags.insert(0, para_tag)
        
    return turnofftags, turnontags

def _getTagsHTML(iter, open_tags):
    (turnofftags, turnontags) = _getTags(iter)

    html = ""
    
    html = html +  _turnOffTags(turnofftags, open_tags)
    html = html + _turnOnTags(turnontags, open_tags)

    return html

def _turnOnTags(turnontags, open_tags):
    html = ""
    for tag in turnontags:
        open_tags.append(tag)
        html = html + tag.opening_tag
    return html

def _turnOffTags(turnofftags, open_tags):
    html = ""
    for tag in turnofftags:
        tags_to_reopen = []
        opentag = open_tags.pop()
        while opentag != tag:
            html = html + opentag.closing_tag
            tags_to_reopen.append(opentag)
            opentag = open_tags.pop()

        html = html + tag.closing_tag
        html = html + _turnOnTags(tags_to_reopen, open_tags)

    return html