diff options
| author | Mike Buland <eichlan@xagasoft.com> | 2012-01-06 00:42:36 -0700 |
|---|---|---|
| committer | Mike Buland <eichlan@xagasoft.com> | 2012-01-06 00:42:36 -0700 |
| commit | 3af6cc33ff246d31513b9a645403a9ece58983a0 (patch) | |
| tree | 47f917ba1bb8a092ec0254e53383d3cf22dae6bc /src | |
| parent | de83196633b28f06bb8c08ddf5780cc8049703f4 (diff) | |
| download | stage-3af6cc33ff246d31513b9a645403a9ece58983a0.tar.gz stage-3af6cc33ff246d31513b9a645403a9ece58983a0.tar.bz2 stage-3af6cc33ff246d31513b9a645403a9ece58983a0.tar.xz stage-3af6cc33ff246d31513b9a645403a9ece58983a0.zip | |
Two experimental SML converters.
Diffstat (limited to 'src')
| -rw-r--r-- | src/options.cpp | 186 | ||||
| -rw-r--r-- | src/smlnode.cpp | 4 |
2 files changed, 190 insertions, 0 deletions
diff --git a/src/options.cpp b/src/options.cpp index 1cc76ce..0f84e1c 100644 --- a/src/options.cpp +++ b/src/options.cpp | |||
| @@ -67,6 +67,189 @@ int Options::builtins( Bu::StrArray aArgs ) | |||
| 67 | return 0; | 67 | return 0; |
| 68 | } | 68 | } |
| 69 | 69 | ||
| 70 | void smlToHtml( const SmlNode *pNode ) | ||
| 71 | { | ||
| 72 | switch( pNode->getType() ) | ||
| 73 | { | ||
| 74 | case SmlNode::typeRoot: | ||
| 75 | sio << "<p>"; | ||
| 76 | for( SmlNode::SmlNodeList::const_iterator i = | ||
| 77 | pNode->getChildren().begin(); i; i++ ) | ||
| 78 | { | ||
| 79 | smlToHtml( *i ); | ||
| 80 | } | ||
| 81 | sio << "</p>"; | ||
| 82 | break; | ||
| 83 | |||
| 84 | case SmlNode::typeText: | ||
| 85 | sio << pNode->getText(); | ||
| 86 | break; | ||
| 87 | |||
| 88 | case SmlNode::typeTag: | ||
| 89 | if( pNode->getChildren().isEmpty() ) | ||
| 90 | { | ||
| 91 | if( pNode->getText() == "break" ) | ||
| 92 | sio << "</p><p>"; | ||
| 93 | } | ||
| 94 | else | ||
| 95 | { | ||
| 96 | if( pNode->getText() == "red" ) | ||
| 97 | { | ||
| 98 | sio << "<span style=\"color: red;\">"; | ||
| 99 | for( SmlNode::SmlNodeList::const_iterator i = | ||
| 100 | pNode->getChildren().begin(); i; i++ ) | ||
| 101 | { | ||
| 102 | smlToHtml( *i ); | ||
| 103 | } | ||
| 104 | sio << "</span>"; | ||
| 105 | } | ||
| 106 | else if( pNode->getText() == "green" ) | ||
| 107 | { | ||
| 108 | sio << "<span style=\"color: green;\">"; | ||
| 109 | for( SmlNode::SmlNodeList::const_iterator i = | ||
| 110 | pNode->getChildren().begin(); i; i++ ) | ||
| 111 | { | ||
| 112 | smlToHtml( *i ); | ||
| 113 | } | ||
| 114 | sio << "</span>"; | ||
| 115 | } | ||
| 116 | } | ||
| 117 | break; | ||
| 118 | } | ||
| 119 | } | ||
| 120 | |||
| 121 | void appendToken( Bu::String &sCurLine, Bu::String &sNextToken, int &iLineLen ) | ||
| 122 | { | ||
| 123 | if( iLineLen + sNextToken.getSize() + 1 >= 78 ) | ||
| 124 | { | ||
| 125 | sio << sCurLine << sio.nl; | ||
| 126 | iLineLen = 0; | ||
| 127 | sCurLine = sNextToken; | ||
| 128 | |||
| 129 | } | ||
| 130 | else | ||
| 131 | { | ||
| 132 | sCurLine += sNextToken; | ||
| 133 | } | ||
| 134 | iLineLen += sNextToken.getSize() + 1; | ||
| 135 | sCurLine += " "; | ||
| 136 | sNextToken.clear(); | ||
| 137 | } | ||
| 138 | |||
| 139 | void smlToConsole( const SmlNode *pNode, Bu::String &sCurLine, | ||
| 140 | Bu::String &sNextToken, int &iLineLen, int &iState ) | ||
| 141 | { | ||
| 142 | // sio << "Begin: iState = " << iState << sio.nl; | ||
| 143 | switch( pNode->getType() ) | ||
| 144 | { | ||
| 145 | case SmlNode::typeRoot: | ||
| 146 | for( SmlNode::SmlNodeList::const_iterator i = | ||
| 147 | pNode->getChildren().begin(); i; i++ ) | ||
| 148 | { | ||
| 149 | smlToConsole( *i, sCurLine, sNextToken, iLineLen, iState ); | ||
| 150 | } | ||
| 151 | break; | ||
| 152 | |||
| 153 | case SmlNode::typeText: | ||
| 154 | { | ||
| 155 | Bu::String::const_iterator iBgn = pNode->getText().begin(); | ||
| 156 | Bu::String::const_iterator iEnd = iBgn; | ||
| 157 | for(;iBgn;) | ||
| 158 | { | ||
| 159 | // sio << iState << ": [" << (iBgn?*iBgn:'-') << ":" | ||
| 160 | // << (iEnd?*iEnd:'-') << "]" | ||
| 161 | // << sio.nl; | ||
| 162 | int iTmpLen = 0; | ||
| 163 | switch( iState ) | ||
| 164 | { | ||
| 165 | case 0: // begining of paragraph | ||
| 166 | if( iBgn && ( *iBgn == ' ' || *iBgn == '\n' || | ||
| 167 | *iBgn == '\r' || *iBgn == '\t' ) ) | ||
| 168 | { | ||
| 169 | iBgn++; | ||
| 170 | } | ||
| 171 | else | ||
| 172 | { | ||
| 173 | // Here is where you would indent paragraphs | ||
| 174 | iEnd = iBgn; | ||
| 175 | iState = 1; | ||
| 176 | } | ||
| 177 | break; | ||
| 178 | |||
| 179 | case 1: // non-whitespace | ||
| 180 | if( !iEnd ) | ||
| 181 | { | ||
| 182 | sNextToken.append( iBgn, iEnd ); | ||
| 183 | iBgn = iEnd; | ||
| 184 | } | ||
| 185 | else if( *iEnd == ' ' || *iEnd == '\n' || | ||
| 186 | *iEnd == '\r' || *iEnd == '\t' ) | ||
| 187 | { | ||
| 188 | sNextToken.append( iBgn, iEnd ); | ||
| 189 | iState = 2; | ||
| 190 | iBgn = iEnd; | ||
| 191 | } | ||
| 192 | else | ||
| 193 | { | ||
| 194 | iEnd++; | ||
| 195 | iTmpLen++; | ||
| 196 | } | ||
| 197 | break; | ||
| 198 | |||
| 199 | case 2: // Whitespace | ||
| 200 | if( iBgn && (*iBgn == ' ' || *iBgn == '\n' || | ||
| 201 | *iBgn == '\r' || *iBgn == '\t') ) | ||
| 202 | { | ||
| 203 | iBgn++; | ||
| 204 | } | ||
| 205 | else | ||
| 206 | { | ||
| 207 | iEnd = iBgn; | ||
| 208 | iState = 1; | ||
| 209 | appendToken( sCurLine, sNextToken, iLineLen ); | ||
| 210 | } | ||
| 211 | break; | ||
| 212 | } | ||
| 213 | } | ||
| 214 | } | ||
| 215 | break; | ||
| 216 | |||
| 217 | case SmlNode::typeTag: | ||
| 218 | if( pNode->getChildren().isEmpty() ) | ||
| 219 | { | ||
| 220 | if( pNode->getText() == "break" ) | ||
| 221 | { | ||
| 222 | appendToken( sCurLine, sNextToken, iLineLen ); | ||
| 223 | if( !sCurLine.isEmpty() ) | ||
| 224 | sio << sCurLine << sio.nl; | ||
| 225 | sCurLine.clear(); | ||
| 226 | iLineLen = 0; | ||
| 227 | iState = 0; | ||
| 228 | } | ||
| 229 | } | ||
| 230 | else | ||
| 231 | { | ||
| 232 | for( SmlNode::SmlNodeList::const_iterator i = | ||
| 233 | pNode->getChildren().begin(); i; i++ ) | ||
| 234 | { | ||
| 235 | smlToConsole( *i, sCurLine, sNextToken, iLineLen, iState ); | ||
| 236 | } | ||
| 237 | } | ||
| 238 | break; | ||
| 239 | } | ||
| 240 | |||
| 241 | // sio << "Green? \x1b[1mhello\x1b[0m huh" << sio.nl; | ||
| 242 | } | ||
| 243 | |||
| 244 | void smlToConsole( const SmlNode *pNode ) | ||
| 245 | { | ||
| 246 | Bu::String sBuf, sBuf2; | ||
| 247 | int i1 = 0, i2 = 0; | ||
| 248 | smlToConsole( pNode, sBuf, sBuf2, i1, i2 ); | ||
| 249 | appendToken( sBuf, sBuf2, i1 ); | ||
| 250 | sio << sBuf << sBuf2 << sio.nl; | ||
| 251 | } | ||
| 252 | |||
| 70 | int Options::smlTest( Bu::Array<Bu::String> aArgs ) | 253 | int Options::smlTest( Bu::Array<Bu::String> aArgs ) |
| 71 | { | 254 | { |
| 72 | Bu::String sContent; | 255 | Bu::String sContent; |
| @@ -80,6 +263,9 @@ int Options::smlTest( Bu::Array<Bu::String> aArgs ) | |||
| 80 | 263 | ||
| 81 | SmlNode *pRoot = SmlNode::parse( sContent ); | 264 | SmlNode *pRoot = SmlNode::parse( sContent ); |
| 82 | sio << *pRoot << sio.nl; | 265 | sio << *pRoot << sio.nl; |
| 266 | |||
| 267 | smlToConsole( pRoot ); | ||
| 268 | |||
| 83 | delete pRoot; | 269 | delete pRoot; |
| 84 | 270 | ||
| 85 | exit( 0 ); | 271 | exit( 0 ); |
diff --git a/src/smlnode.cpp b/src/smlnode.cpp index a9e533d..e2f8a74 100644 --- a/src/smlnode.cpp +++ b/src/smlnode.cpp | |||
| @@ -13,6 +13,10 @@ SmlNode::SmlNode( Type eType, const Bu::String &sText, SmlNode *pParent ) : | |||
| 13 | 13 | ||
| 14 | SmlNode::~SmlNode() | 14 | SmlNode::~SmlNode() |
| 15 | { | 15 | { |
| 16 | for( SmlNodeList::iterator i = lChildren.begin(); i; i++ ) | ||
| 17 | { | ||
| 18 | delete *i; | ||
| 19 | } | ||
| 16 | } | 20 | } |
| 17 | 21 | ||
| 18 | SmlNode *SmlNode::parse( const Bu::String &sSrc ) | 22 | SmlNode *SmlNode::parse( const Bu::String &sSrc ) |
