Brian Silverman | 70325d6 | 2015-09-20 17:00:43 -0400 | [diff] [blame^] | 1 | // Copyright (c) 2009, Google Inc. |
| 2 | // All rights reserved. |
| 3 | // |
| 4 | // Redistribution and use in source and binary forms, with or without |
| 5 | // modification, are permitted provided that the following conditions are |
| 6 | // met: |
| 7 | // |
| 8 | // * Redistributions of source code must retain the above copyright |
| 9 | // notice, this list of conditions and the following disclaimer. |
| 10 | // * Redistributions in binary form must reproduce the above |
| 11 | // copyright notice, this list of conditions and the following disclaimer |
| 12 | // in the documentation and/or other materials provided with the |
| 13 | // distribution. |
| 14 | // * Neither the name of Google Inc. nor the names of its |
| 15 | // contributors may be used to endorse or promote products derived from |
| 16 | // this software without specific prior written permission. |
| 17 | // |
| 18 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | |
| 30 | // --- |
| 31 | |
| 32 | #include <config.h> |
| 33 | #include <ctemplate/template_annotator.h> |
| 34 | #include <string> |
| 35 | #include <ctemplate/template_emitter.h> |
| 36 | |
| 37 | // Emits an open annotation string. 'name' must be a string literal. |
| 38 | #define EMIT_OPEN_ANNOTATION(emitter, name, value) \ |
| 39 | (emitter)->Emit("{{#" name "=", 4 + sizeof(name)-1); \ |
| 40 | (emitter)->Emit(value); \ |
| 41 | (emitter)->Emit("}}", 2); |
| 42 | |
| 43 | // Emits a close annotation string. 'name' must be a string literal. |
| 44 | #define EMIT_CLOSE_ANNOTATION(emitter, name) \ |
| 45 | (emitter)->Emit("{{/" name "}}", 5 + sizeof(name)-1); |
| 46 | |
| 47 | #define EMIT_MISSING_ANNOTATION(emitter, name, value) \ |
| 48 | (emitter)->Emit("{{" name "=", 3 + sizeof(name)-1); \ |
| 49 | (emitter)->Emit(value); \ |
| 50 | (emitter)->Emit("}}", 2); |
| 51 | |
| 52 | namespace ctemplate { |
| 53 | |
| 54 | using std::string; |
| 55 | |
| 56 | // Implementation note: TextTemplateAnnotator contains no state, and |
| 57 | // code elsewhere is depending on this. E.g., a statically allocated |
| 58 | // instance is used as the default annotator in the implementation of |
| 59 | // PerExpandData. If you add state to this class, please revisit |
| 60 | // the setup of such static instances. |
| 61 | |
| 62 | // This implements precisely the same annotation that was originally |
| 63 | // built into the template.cc. Many upstream tools depend on the |
| 64 | // exact formatting that this implementation happens to produce-- |
| 65 | // so do not consider changes to this lightly. |
| 66 | |
| 67 | void TextTemplateAnnotator::EmitOpenInclude(ExpandEmitter* emitter, |
| 68 | const string& value) { |
| 69 | EMIT_OPEN_ANNOTATION(emitter, "INC", value); |
| 70 | } |
| 71 | |
| 72 | void TextTemplateAnnotator::EmitCloseInclude(ExpandEmitter* emitter) { |
| 73 | EMIT_CLOSE_ANNOTATION(emitter, "INC"); |
| 74 | } |
| 75 | |
| 76 | void TextTemplateAnnotator::EmitOpenFile(ExpandEmitter* emitter, |
| 77 | const string& value) { |
| 78 | EMIT_OPEN_ANNOTATION(emitter, "FILE", value); |
| 79 | } |
| 80 | |
| 81 | void TextTemplateAnnotator::EmitCloseFile(ExpandEmitter* emitter) { |
| 82 | EMIT_CLOSE_ANNOTATION(emitter, "FILE"); |
| 83 | } |
| 84 | |
| 85 | void TextTemplateAnnotator::EmitOpenSection(ExpandEmitter* emitter, |
| 86 | const string& value) { |
| 87 | EMIT_OPEN_ANNOTATION(emitter, "SEC", value); |
| 88 | } |
| 89 | |
| 90 | void TextTemplateAnnotator::EmitCloseSection(ExpandEmitter* emitter) { |
| 91 | EMIT_CLOSE_ANNOTATION(emitter, "SEC"); |
| 92 | } |
| 93 | |
| 94 | void TextTemplateAnnotator::EmitOpenVariable(ExpandEmitter* emitter, |
| 95 | const string& value) { |
| 96 | EMIT_OPEN_ANNOTATION(emitter, "VAR", value); |
| 97 | } |
| 98 | |
| 99 | void TextTemplateAnnotator::EmitCloseVariable(ExpandEmitter* emitter) { |
| 100 | EMIT_CLOSE_ANNOTATION(emitter, "VAR"); |
| 101 | } |
| 102 | |
| 103 | void TextTemplateAnnotator::EmitFileIsMissing(ExpandEmitter* emitter, |
| 104 | const string& value) { |
| 105 | EMIT_MISSING_ANNOTATION(emitter,"MISSING_FILE", value); |
| 106 | } |
| 107 | |
| 108 | } |