-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathNote.php
127 lines (108 loc) · 3.99 KB
/
Note.php
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
<?php
require_once 'Note/AssembleRTF.php';
require_once 'Note/BraceLexer.php';
require_once 'Note/SectionLexer.php';
require_once 'Note/RTFToHTML.php';
require_once 'Note/HTMLToRTF.php';
require_once 'Note/RTFToPlainText.php';
class JoshRibakoff_Note
{
const DEFAULT_FONTSIZE = '17';
const DEFAULT_FONT = '0';
const DEFAULT_COLOR = '000000';
protected $rtf_fonttable;
protected $rtf_colortable;
protected $rtf_text_pieces;
protected $raw_rtf;
/** Set the RTF text this Note object should represent. Overwrites any previously set RTF data. */
function setRTF($rtf)
{
$this->raw_rtf = $rtf;
$rtf = $this->stripOuterBraces($rtf);
$tree = $this->convertBracesToArray($rtf);
// find 'text' section (last element of tree array) and escape RTF tokens (brace lexer un-escapes them)
$textSection = $tree[count($tree) - 1];
$textSection = str_replace('{', '\{', $textSection);
$textSection = str_replace('}', '\}', $textSection);
$tree[count($tree) - 1] = $textSection;
$sectionLexer = new JoshRibakoff_Note_SectionLexer;
$sectionLexer->setInput($tree);
$this->rtf_fonttable = $sectionLexer->fontTable();
$this->rtf_colortable = $sectionLexer->colorTable();
$this->rtf_text_pieces = $sectionLexer->textPieces();
$this->rtf_text = $sectionLexer->text();
}
function stripOuterBraces($rtf)
{
if (preg_match('#^\\{(.*)\\}#s', $rtf, $matches)) {
return $matches[1];
}
return $rtf;
}
function convertBracesToArray($rtf)
{
$braceLexer = new JoshRibakoff_Note_BraceLexer;
$braceLexer->setRTF($rtf);
return $braceLexer->tokenize();
}
function colorTable()
{
return $this->rtf_colortable;
}
function text()
{
return $this->rtf_text_pieces;
}
/**
* Takes a line of HTML to prepend to the top of the RTF originally passed to setRTF(). Converts HTML in that
* line to RTF tokens and also updates the RTF document's color table & font table.
*/
function prependNote($noteDataToPrepend)
{
if (!isset($noteDataToPrepend['fontsize'])) {
$noteDataToPrepend['fontsize'] = self::DEFAULT_FONTSIZE;
}
if (!isset($noteDataToPrepend['font'])) {
$noteDataToPrepend['font'] = self::DEFAULT_FONT;
}
$converter = new JoshRibakoff_Note_HTMLToRTF;
$converter->setColorTable($this->rtf_colortable);
$noteDataToPrepend['text'] = $converter->convert($noteDataToPrepend['text']);
$this->rtf_colortable = $converter->newColorTable();
array_unshift($this->rtf_text_pieces, $noteDataToPrepend);
$this->updateRTF();
}
/** Formats the note represented by this object as HTML */
function formatHTML()
{
$converter = new JoshRibakoff_Note_RTFToHTML;
$converter->setColorTable($this->colorTable());
return $converter->convert(trim($this->rtf_text));
}
function formatPlaintext($params = array())
{
$converter = new JoshRibakoff_Note_RTFToPlainText();
if (isset($params['newline'])) {
$converter->setNewline($params['newline']);
}
return $converter->convert($this->rtf_text);
}
/**
* Creates a new RTF document from the Note this object represents. Internally it just assembles "pieces" that were
* lexed & parsed from setRTF(), and sometimes augmented with prependNote(). In that case this will look like the
* original RTF document, with some new RTF data at the top, but before the header.
*/
function formatRTF()
{
$assembler = new JoshRibakoff_Note_AssembleRTF(array(
'fonttable' => $this->rtf_fonttable,
'colortable' => $this->rtf_colortable,
'rtf_text_pieces' => $this->rtf_text_pieces
));
return $assembler->assemble();
}
function updateRTF()
{
$this->setRTF($this->formatRTF());
}
}