Debian Codegraph @

texmf-dist/doc/optex/opbible/txs-gen/mod2tex.cpp

raw
1 /******************************************************************************
2 *
3 * mod2imp.cpp - Utility to export a module in IMP format
4 *
5 * $Id$
6 *
7 * Copyright 2002-2013 CrossWire Bible Society (http://www.crosswire.org)
8 * CrossWire Bible Society
9 * P. O. Box 2528
10 * Tempe, AZ 85280-2528
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation version 2.
15 *
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
20 *
21 */
22
23 #ifdef _MSC_VER
24 #pragma warning( disable: 4251 )
25 #endif
26
27 #include <iostream>
28 #include <map>
29 #include <stdio.h>
30
31 #include <markupfiltmgr.h>
32 #include <swmgr.h>
33 #include <swmodule.h>
34 #include <versekey.h>
35
36 using std::map;
37 using std::cout;
38 using std::endl;
39
40 #ifndef NO_SWORD_NAMESPACE
41
42 using namespace sword;
43
44 #endif
45
46
47 void usage(const char *progName, const char *error = 0) {
48 if (error) fprintf(stderr, "\n%s: %s\n", progName, error);
49 fprintf(stderr, "\n=== mod2imp (Revision $Rev$) SWORD module exporter.\n");
50 fprintf(stderr, "\nusage: %s <module_name> [options]\n"
51 "\t -s - strip markup instead of outputting raw native data.\n"
52 "\t -f <option_name> <option_value> - when rendering (-r, above), option\n"
53 "\t\tfilter values can be set with this option.\n\n"
54 , progName);
55 exit(-1);
56 }
57
58
59 int main(int argc, char **argv)
60 {
61 // handle options
62 if (argc < 2) usage(*argv);
63
64 const char *progName = argv[0];
65 const char *modName = argv[1];
66 bool render = true;
67 bool strip = false;
68 SWBuf renderForm = "PLAIN";
69 SWBuf optionName;
70 map<SWBuf, SWBuf> options; // optionName, optionValue;
71
72 for (int i = 2; i < argc; i++) {
73 if (!strcmp(argv[i], "-s")) {
74 if (render) usage(progName, "-s can't be supplied when using -r");
75 strip = true;
76 }
77 else if (!strcmp(argv[i], "-f")) {
78 if (i+1 < argc) optionName = argv[++i];
79 if (i+1 < argc) options[optionName] = argv[++i];
80 else usage(progName, "-f requires <option_name> <option_value>");
81 }
82 else usage(progName, (((SWBuf)"Unknown argument: ")+ argv[i]).c_str());
83 }
84 // -----------------------------------------------------
85
86 MarkupFilterMgr *markupMgr = new MarkupFilterMgr(sword::FMT_PLAIN);
87
88 SWMgr *mgr = (markupMgr) ? new SWMgr(markupMgr) : new SWMgr();
89
90 // set any options filters passed with -f
91 for (map<SWBuf, SWBuf>::iterator it = options.begin(); it != options.end(); it++) {
92 mgr->setGlobalOption(it->first, it->second);
93 }
94
95 SWModule *module = mgr->getModule(modName);
96
97 if (!module) usage(progName, (((SWBuf) "Couldn't find module: ") + modName).c_str());
98
99
100 SWKey *key = module->getKey();
101 VerseKey *vkey = SWDYNAMIC_CAST(VerseKey, key);
102
103 if (vkey)
104 vkey->setIntros(true);
105
106 char book = -1; // init nonexistent value
107
108 for ((*module) = TOP; !module->popError(); (*module)++) {
109 if (!vkey->getBook()) { continue; }
110 vkey = SWDYNAMIC_CAST(VerseKey, module->getKey());
111 if (book != vkey->getBook()) {
112 std::cout << "\\kniha{" << vkey->getBookName() << "}" << std::endl;
113 std::cout << "\\zkratka{" << vkey->getBookAbbrev() << "}" << std::endl;
114 book = vkey->getBook();
115 }
116 if (!vkey->getChapter()) { continue; }
117 if (!vkey->getVerse()) { continue; }
118 std::cout << '<' << vkey->getChapter() << ':' << vkey->getVerse() << ' ' << ((render) ? module->renderText().c_str() : (strip) ? module->stripText() : module->getRawEntry()) << '>' << std::endl;
119 }
120
121 cout << endl;
122
123 return 0;
124 }