Repository navigation
Expand file tree
/
Copy pathsd_BBcodeEditor.cpp
More file actions
174 lines (153 loc) · 6.05 KB
/
Copy pathsd_BBcodeEditor.cpp
File metadata and controls
174 lines (153 loc) · 6.05 KB
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
/*
=====================================================================================================
sd_BBCodeEditor.cpp
Created : 10 Apr 2022 1:08:13am
Author : Marcel Huibers
Project : SD Toolkit
Company : Sound Development
Copyright: Marcel Huibers (c) 2022 All Rights Reserved
=====================================================================================================
*/
namespace sd
{
void BBCodeEditor::initialise( )
{
setJustification( kDefaultJustification );
m_listPrefix = false;
m_quotePrefix = false;
m_stateQueue.clear();
m_stateQueue.emplace_back( sd::TextFormatState( findColour( juce::TextEditor::textColourId ) ) );
clear();
}
//==============================================================================
void BBCodeEditor::setBBText( const juce::String& bbText )
{
initialise();
auto plainText = bbText.upToFirstOccurrenceOf( BBCode::kTokenStart, false, false );
auto remainingText = bbText.fromFirstOccurrenceOf( BBCode::kTokenStart, false, false );
addBBText( plainText );
while( remainingText.isNotEmpty() )
{
// Ignore superfluous '['...
if( remainingText.startsWith( BBCode::kTokenStart ) )
{
remainingText = remainingText.substring( 1 );
addBBText( BBCode::kTokenStart );
continue;
}
// Check for 'quote' tokens...
juce::String value {};
if( remainingText.startsWith( BBCode::kQuoteToken ) )
{
value = remainingText.upToFirstOccurrenceOf( BBCode::kTokenEnd, false, false ).fromFirstOccurrenceOf( BBCode::kValueDelimiter, false, false );
m_quotePrefix = true;
}
// Process lists...
bool succesfullyParsed = true;
if( remainingText.startsWith( BBCode::kBulletToken ) )
{
remainingText = remainingText.substring( 1 );
m_listPrefix = true;
}
else
{
const auto token = remainingText.upToFirstOccurrenceOf( BBCode::kTokenEnd, false, false );
if( token.contains( BBCode::kTokenStart ) )
{
succesfullyParsed = false;
}
// Parse justification (juce::TextEditor only has global justification)...
else if( token.startsWith( BBCode::kAlignToken ) )
{
parseJustification( token, !token.startsWith( BBCode::kCloseTokenPrefix ) );
}
// Parse other tokens...
else if( auto newState = m_stateQueue.back().withToken( token ) )
{
// End token pops state...
if( token.startsWith( BBCode::kCloseTokenPrefix ) )
{
if( m_stateQueue.size() > 1 )
m_stateQueue.pop_back();
}
// Start token pushes state...
else
{
m_stateQueue.emplace_back( newState.operator*() );
}
}
else
{
succesfullyParsed = false;
}
}
// Assemble plain text to be added to the editor...
if( succesfullyParsed )
plainText = remainingText.fromFirstOccurrenceOf( BBCode::kTokenEnd, false, false ).upToFirstOccurrenceOf( BBCode::kTokenStart, false, false );
else
plainText = BBCode::kTokenStart + remainingText.upToFirstOccurrenceOf( BBCode::kTokenStart, false, false );
// Trailing newlines for CODE blocks...
if( remainingText.startsWith( BBCode::kCodeToken ) )
plainText = juce::newLine + juce::newLine + kTabCharacter + plainText + juce::newLine + juce::newLine;
addBBText( plainText, value );
// Progress the parser....
remainingText = remainingText.fromFirstOccurrenceOf( BBCode::kTokenStart, false, false );
}
}
//==============================================================================
void BBCodeEditor::addBBText( const juce::String& text, const juce::String& value /*= {}*/ )
{
if( text.isEmpty() )
return;
sd::TextFormatState textFormatState { findColour( juce::TextEditor::textColourId ) }; // Default state.
if( !m_stateQueue.empty() )
textFormatState = m_stateQueue.back();
setColour( juce::TextEditor::textColourId, textFormatState.getColour() );
setFont( textFormatState.getFont() );
moveCaretToEnd();
// Add quote...
if( m_quotePrefix )
{
insertTextAtCaret( juce::newLine + juce::newLine + "|" + kTabCharacter );
if( value.isNotEmpty() )
{
const auto previousFont = getFont();
const auto wasBold = previousFont.isBold();
setFont( previousFont.boldened() );
insertTextAtCaret( value + ": " );
if( !wasBold )
setFont( previousFont.withStyle( previousFont.getStyleFlags() & ~juce::Font::bold ) ); // NOLINT
}
insertTextAtCaret( juce::String::fromUTF8( kOpenQuotes ) + text + juce::String::fromUTF8( kCloseQuotes ) );
}
// Add bullet list item...
else if( m_listPrefix )
{
insertTextAtCaret( juce::String::fromUTF8( kBulletCharacter ) + kTabCharacter + text );
}
// Add plain text...
else
{
insertTextAtCaret( text );
}
m_listPrefix = false;
m_quotePrefix = false;
}
//==============================================================================
void BBCodeEditor::parseJustification( const juce::String& token, bool enable )
{
m_justification = kDefaultJustification;
if( enable )
{
if( token.endsWithIgnoreCase( "right" ) )
m_justification = juce::Justification::topRight;
else if( token.endsWithIgnoreCase( "center" ) )
m_justification = juce::Justification::centredTop;
else if( token.endsWithIgnoreCase( "justify" ) )
m_justification = juce::Justification::horizontallyJustified;
else
m_justification = juce::Justification::topLeft;
}
setJustification( m_justification );
}
} // namespace sd