Cee Plus Plus Markup Language

If you need to invent a new markup language for some of your documents, you can just use your CeePlusPlus compiler to carry out the transformations.

First, define a DocumentTypeDefinition of sorts by defining a header file.

 // dtd.h

void beginDocument(); void beginSection(char * name); void sectionText(char * text); void endSection(); void endDocument();

Then, write your document.

 // document.h

void myDocument();

// document.cpp

#include "dtd.h"

void myDocument() { beginDocument(); beginSection("Section One"); sectionText("This is one of my sections." " I forgot to define paragraphs."); endSection(); beginSection("Section Two"); sectionText("Here is another section."); endSection(); endDocument(); };

Then, define your functions to produce formatting:

 // stylesheet.cpp

#include <iostream> using namespace std;

#include "dtd.h"

void beginDocument() { cout << "<html><head><title>Formatting example</title><head>" << endl << "<body text=black bgcolor=\"#E0E0E0\">" << endl; }

void beginSection(char * name) { cout << "<h1 align=center>" << name << "</h1>" << endl << "<p>"; }

void sectionText(char * text) { cout << text << endl; }

void endSection() { cout << "</p>" << endl; }

void endDocument() { cout << "</body></html>" << endl; }

This is just a sample.

Then you write a main function to bring this all together.

 // main.cpp

#include "document.h"

int main(void) { myDocument(); }

You might want an exception handler if you're doing validation checking. I skipped it, here.

You compile all this together using GnuCpp:

 g++ -c document.cpp -o document.o
 g++ -c stylesheet.cpp -o stylesheet.o
 g++ -c main.cpp -o main.o
 g++ document.o stylesheet.o main.o -o makedoc
 makedoc > doc.html
 rm makedoc

Then you've got your document in HTML format. Voila! But with different stylesheets, you can produce any format you want.

-- EdwardKiser


Brilliant! I believe this example, because it is so obvious, could help many a programmer to a deeper understanding of the difference (or similarity!) between code and data. -- DanielBrockman


Yuck! You're way overstretching the idea of MarkUp. A key goal of template mechanisms is "inverting the sock", ie, inserting code into text (data) as opposed to inserting constant text into source code, for convenience or whatever. Markup means to add syntax to (otherwise plain-looking) text to represent structure, or other semantics (I hate this word), for similar purposes (cf definition of MarkUp). What you've done here is merely inserting constant strings into an algorithm (ie, code). The separation-of-concerns achieved by modularizing, here used for transforming the text, doesn't surprise me at all. Trivial. And ugly!

(But if you'd done this in Python... ;o)


CategoryCee CategoryCpp MarkupLanguage


EditText of this page (last edited December 16, 2006) or FindPage with title or text search