[sword-svn] r3071 - in trunk: include src/modules/filters

refdoc at crosswire.org refdoc at crosswire.org
Tue Mar 4 17:10:11 MST 2014


Author: refdoc
Date: 2014-03-04 17:10:10 -0700 (Tue, 04 Mar 2014)
New Revision: 3071

Added:
   trunk/src/modules/filters/teilatex.cpp
Modified:
   trunk/include/Makefile.am
   trunk/include/teilatex.h
Log:
Tei filter for LaTeX now based on xhtml - needs fleshing out



Modified: trunk/include/Makefile.am
===================================================================
--- trunk/include/Makefile.am	2014-03-05 00:07:12 UTC (rev 3070)
+++ trunk/include/Makefile.am	2014-03-05 00:10:10 UTC (rev 3071)
@@ -138,6 +138,7 @@
 pkginclude_HEADERS += $(swincludedir)/teirtf.h
 pkginclude_HEADERS += $(swincludedir)/teixhtml.h
 pkginclude_HEADERS += $(swincludedir)/teihtmlhref.h
+pkginclude_HEADERS += $(swincludedir)/teilatex.h
 
 pkginclude_HEADERS += $(swincludedir)/treekey.h
 pkginclude_HEADERS += $(swincludedir)/treekeyidx.h

Modified: trunk/include/teilatex.h
===================================================================
--- trunk/include/teilatex.h	2014-03-05 00:07:12 UTC (rev 3070)
+++ trunk/include/teilatex.h	2014-03-05 00:10:10 UTC (rev 3071)
@@ -1,10 +1,10 @@
-/***************************************************************************
+/******************************************************************************
  *
  *  teilatex.h -	Implementation of TEILaTeX
  *
- * $Id: gbflatex.h 2998 2013-12-30 13:10:35Z chrislit $
+ * $Id: teixhtml.h 2833 2013-06-29 06:40:28Z chrislit $
  *
- * Copyright 2014 CrossWire Bible Society (http://www.crosswire.org)
+ * Copyright 2012-2013 CrossWire Bible Society (http://www.crosswire.org)
  *	CrossWire Bible Society
  *	P. O. Box 2528
  *	Tempe, AZ  85280-2528
@@ -20,14 +20,36 @@
  *
  */
 
-#ifndef TEILATEX_H
-#define TEILATEX_H
+#ifndef TEILaTeX_H
+#define TEILaTeX_H
 
+#include <swbasicfilter.h>
+
 SWORD_NAMESPACE_START
 
+/** this filter converts TEI text to XHTML text
+ */
+class SWDLLEXPORT TEILaTeX : public SWBasicFilter {
+private:
+	bool renderNoteNumbers;
 
+protected:
+	class MyUserData : public BasicFilterUserData {
+	public:
+		bool BiblicalText;
+		SWBuf lastHi;
+		
+		SWBuf version;
+		MyUserData(const SWModule *module, const SWKey *key);
+	};
+	virtual BasicFilterUserData *createUserData(const SWModule *module, const SWKey *key) {
+		return new MyUserData(module, key);
+	}
+	virtual bool handleToken(SWBuf &buf, const char *token, BasicFilterUserData *userData);
+public:
+	TEILaTeX();
+	void setRenderNoteNumbers(bool val = true) { renderNoteNumbers = val; }
+};
+
 SWORD_NAMESPACE_END
 #endif
-
-
-

Added: trunk/src/modules/filters/teilatex.cpp
===================================================================
--- trunk/src/modules/filters/teilatex.cpp	                        (rev 0)
+++ trunk/src/modules/filters/teilatex.cpp	2014-03-05 00:10:10 UTC (rev 3071)
@@ -0,0 +1,290 @@
+/***************************************************************************
+ *
+ *  teilatex.cpp -	TEI to LATEX filter
+ *
+ * $Id: teilatex.cpp 2984 2013-09-20 12:18:45Z refdoc $
+ *
+ * Copyright 2012-2013 CrossWire Bible Society (http://www.crosswire.org)
+ *	CrossWire Bible Society
+ *	P. O. Box 2528
+ *	Tempe, AZ  85280-2528
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation version 2.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ */
+
+#include <stdlib.h>
+#include <ctype.h>
+#include <teilatex.h>
+#include <utilxml.h>
+#include <swmodule.h>
+#include <url.h>
+#include <iostream>
+
+
+SWORD_NAMESPACE_START
+
+
+TEILaTeX::MyUserData::MyUserData(const SWModule *module, const SWKey *key) : BasicFilterUserData(module, key) {
+	BiblicalText = false;
+	if (module) {
+		version = module->getName();
+		BiblicalText = (!strcmp(module->getType(), "Biblical Texts"));
+	}
+}
+
+
+TEILaTeX::TEILaTeX() {
+	setTokenStart("<");
+	setTokenEnd(">");
+
+	setEscapeStart("&");
+	setEscapeEnd(";");
+
+	setEscapeStringCaseSensitive(true);
+
+	addAllowedEscapeString("quot");
+	addAllowedEscapeString("apos");
+	addAllowedEscapeString("amp");
+	addAllowedEscapeString("lt");
+	addAllowedEscapeString("gt");
+
+	setTokenCaseSensitive(true);
+
+	renderNoteNumbers = false;
+}
+
+bool TEILaTeX::handleToken(SWBuf &buf, const char *token, BasicFilterUserData *userData) {
+  // manually process if it wasn't a simple substitution
+	if (!substituteToken(buf, token)) {
+		MyUserData *u = (MyUserData *)userData;
+		XMLTag tag(token);
+
+		if (!strcmp(tag.getName(), "p")) {
+			if ((!tag.isEndTag()) && (!tag.isEmpty())) {	// non-empty start tag
+				buf += "<!P><br />";
+			}
+			else if (tag.isEndTag()) {	// end tag
+				buf += "<!/P><br />";
+				//userData->supressAdjacentWhitespace = true;
+			}
+			else {					// empty paragraph break marker
+				buf += "<!P><br />";
+				//userData->supressAdjacentWhitespace = true;
+			}
+		}
+		
+		// <hi>
+		else if (!strcmp(tag.getName(), "hi")) {
+			if ((!tag.isEndTag()) && (!tag.isEmpty())) {
+				SWBuf rend = tag.getAttribute("rend");
+				
+				u->lastHi = rend;
+				if (rend == "ital")
+					buf += "<i>";
+				else if (rend == "italic")
+					buf += "<i>";
+				else if (rend == "bold")
+					buf += "<b>";
+				else if (rend == "sup")
+					buf += "<small><sup>";
+				else if (rend == "overline")
+					buf += "<span style=\"text-decoration:overline\">";
+
+			}
+			else if (tag.isEndTag()) {
+				SWBuf rend = u->lastHi;
+				if (rend == "ital")
+					buf += "</i>";
+				else if (rend == "italic")
+					buf += "</i>";
+				else if (rend == "bold")
+					buf += "</b>";
+				else if (rend == "sup")
+					buf += "</sup></small>";
+				else if (rend == "overline")
+					buf += "</span>";
+			}
+		}
+
+		// <entryFree>
+		else if (!strcmp(tag.getName(), "entryFree")) {
+			if ((!tag.isEndTag()) && (!tag.isEmpty())) {
+				SWBuf n = tag.getAttribute("n");				
+				if (n != "") {
+					buf += "<b>";
+					buf += n;
+					buf += "</b>";
+				}
+			}
+		}
+
+		// <sense>
+		else if (!strcmp(tag.getName(), "sense")) {
+			if ((!tag.isEndTag()) && (!tag.isEmpty())) {
+				SWBuf n = tag.getAttribute("n");
+				if (n != "") {
+					buf += "<br /><b>";
+					buf += n;
+					buf += "</b>";
+				}
+			}
+		}
+
+		// <div>
+		else if (!strcmp(tag.getName(), "div")) {
+
+			if ((!tag.isEndTag()) && (!tag.isEmpty())) {
+				buf += "<!P>";
+			}
+			else if (tag.isEndTag()) {
+			}
+		}
+
+		// <lb.../>
+		else if (!strcmp(tag.getName(), "lb")) {
+			buf += "<br />";
+		}
+
+		// <pos>, <gen>, <case>, <gram>, <number>, <mood>, <pron>, <def>
+		else if (!strcmp(tag.getName(), "pos") || 
+				 !strcmp(tag.getName(), "gen") || 
+				 !strcmp(tag.getName(), "case") || 
+				 !strcmp(tag.getName(), "gram") || 
+				 !strcmp(tag.getName(), "number") || 
+				 !strcmp(tag.getName(), "pron") /*||
+				 !strcmp(tag.getName(), "def")*/) {
+			if ((!tag.isEndTag()) && (!tag.isEmpty())) {
+				buf += "<i>";
+			}
+			else if (tag.isEndTag()) {
+				buf += "</i>";
+			}
+		}
+
+		// <tr>
+		else if (!strcmp(tag.getName(), "tr")) {
+			if ((!tag.isEndTag()) && (!tag.isEmpty())) {
+				buf += "<i>";
+			}
+			else if (tag.isEndTag()) {
+				buf += "</i>";
+			}
+		}
+		
+		// orth
+		else if (!strcmp(tag.getName(), "orth")) {
+			if ((!tag.isEndTag()) && (!tag.isEmpty())) {
+				buf += "<b>";
+			}
+			else if (tag.isEndTag()) {
+				buf += "</b>";
+			}
+		}
+
+		// <etym>, <usg>
+		else if (!strcmp(tag.getName(), "etym") || 
+				 !strcmp(tag.getName(), "usg")) {
+			// do nothing here
+		}
+		else if (!strcmp(tag.getName(), "ref")) {
+			if (!tag.isEndTag()) {
+				u->suspendTextPassThru = true;
+				SWBuf target;
+				SWBuf work;
+				SWBuf ref;
+
+				int was_osisref = false;
+				if(tag.getAttribute("osisRef"))
+				{
+					target += tag.getAttribute("osisRef");
+					was_osisref=true;
+				}
+				else if(tag.getAttribute("target"))
+					target += tag.getAttribute("target");
+
+				if(target.size())
+				{
+					const char* the_ref = strchr(target, ':');
+					
+					if(!the_ref) {
+						// No work
+						ref = target;
+					}
+					else {
+						// Compensate for starting :
+						ref = the_ref + 1;
+
+						int size = target.size() - ref.size() - 1;
+						work.setSize(size);
+						strncpy(work.getRawData(), target, size);
+					}
+
+					if(was_osisref)
+					{
+						buf.appendFormatted("<a href=\"passagestudy.jsp?action=showRef&type=scripRef&value=%s&module=%s\">",
+							(ref) ? URL::encode(ref.c_str()).c_str() : "", 
+							(work.size()) ? URL::encode(work.c_str()).c_str() : "");
+					}
+					else
+					{
+						// Dictionary link, or something
+						buf.appendFormatted("<a href=\"sword://%s/%s\">",
+							(work.size()) ? URL::encode(work.c_str()).c_str() : u->version.c_str(),
+							(ref) ? URL::encode(ref.c_str()).c_str() : ""							
+							);
+					}
+				}
+				else
+				{
+					//std::cout << "TARGET WASN'T\n";
+				}
+				
+			}
+			else {
+				buf += u->lastTextNode.c_str();
+				buf += "</a>";
+				
+				u->suspendTextPassThru = false;
+			}
+		}
+
+	   	// <note> tag
+		else if (!strcmp(tag.getName(), "note")) {
+			if (!tag.isEndTag()) {
+				if (!tag.isEmpty()) {
+					u->suspendTextPassThru = true;
+				}
+			}
+			if (tag.isEndTag()) {
+				SWBuf footnoteNumber = tag.getAttribute("swordFootnote");
+				SWBuf noteName = tag.getAttribute("n");
+				
+				buf.appendFormatted("<a href=\"passagestudy.jsp?action=showNote&type=n&value=%s&module=%s&passage=%s\"><small><sup class=\"n\">*n%s</sup></small></a>",
+					URL::encode(footnoteNumber.c_str()).c_str(), 
+					URL::encode(u->version.c_str()).c_str(),
+					URL::encode(u->key->getText()).c_str(), 
+					(renderNoteNumbers ? URL::encode(noteName.c_str()).c_str() : ""));
+				
+				u->suspendTextPassThru = false;
+			}
+		}
+
+		else {
+			return false;  // we still didn't handle token
+		}
+
+	}
+	return true;
+}
+
+
+SWORD_NAMESPACE_END
+




More information about the sword-cvs mailing list