The SWORD Project  1.9.0.svnversion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
mod2imp.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  *
3  * mod2imp.cpp - Utility to export a module in IMP format
4  *
5  * $Id: mod2imp.cpp 3088 2014-03-09 13:09:00Z refdoc $
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: 3088 $) SWORD module exporter.\n");
50  fprintf(stderr, "\nusage: %s <module_name> [options]\n"
51  "\t -r [output_format] - render content instead of outputting raw native\n"
52  "\t\tdata. output_format can be: OSIS, XHTML, LATEX, HTMLHREF, RTF.\n"
53  "\t -s - strip markup instead of outputting raw native data.\n"
54  "\t -f <option_name> <option_value> - when rendering (-r, above), option\n"
55  "\t\tfilter values can be set with this option.\n\n"
56  , progName);
57  exit(-1);
58 }
59 
60 
61 int main(int argc, char **argv)
62 {
63  // handle options
64  if (argc < 2) usage(*argv);
65 
66  const char *progName = argv[0];
67  const char *modName = argv[1];
68  bool render = false;
69  bool strip = false;
70  SWBuf renderForm;
71  SWBuf optionName;
72  map<SWBuf, SWBuf> options; // optionName, optionValue;
73 
74  for (int i = 2; i < argc; i++) {
75  if (!strcmp(argv[i], "-r")) {
76  if (strip) usage(progName, "-r can't be supplied when using -s");
77  if (i+1 < argc) renderForm = argv[++i];
78  render = true;
79  }
80  else if (!strcmp(argv[i], "-s")) {
81  if (render) usage(progName, "-s can't be supplied when using -r");
82  strip = true;
83  }
84  else if (!strcmp(argv[i], "-f")) {
85  if (i+1 < argc) optionName = argv[++i];
86  if (i+1 < argc) options[optionName] = argv[++i];
87  else usage(progName, "-f requires <option_name> <option_value>");
88  }
89  else usage(progName, (((SWBuf)"Unknown argument: ")+ argv[i]).c_str());
90  }
91  // -----------------------------------------------------
92 
93  MarkupFilterMgr *markupMgr = 0;
94  if (renderForm == "HTMLHREF") markupMgr = new MarkupFilterMgr(sword::FMT_HTMLHREF);
95  else if (renderForm == "OSIS") markupMgr = new MarkupFilterMgr(sword::FMT_OSIS);
96  else if (renderForm == "RTF") markupMgr = new MarkupFilterMgr(sword::FMT_RTF);
97  else if (renderForm == "LATEX") markupMgr = new MarkupFilterMgr(sword::FMT_LATEX);
98  else if (renderForm == "XHTML") markupMgr = new MarkupFilterMgr(sword::FMT_XHTML);
99 
100  else if (renderForm.length()) usage(progName, (((SWBuf) "Unknown output_format for -r (")+renderForm+")").c_str());
101 
102  SWMgr *mgr = (markupMgr) ? new SWMgr(markupMgr) : new SWMgr();
103 
104  // set any options filters passed with -f
105  for (map<SWBuf, SWBuf>::iterator it = options.begin(); it != options.end(); it++) {
106  mgr->setGlobalOption(it->first, it->second);
107  }
108 
109  SWModule *module = mgr->getModule(modName);
110 
111  if (!module) usage(progName, (((SWBuf) "Couldn't find module: ") + modName).c_str());
112 
113 
114  SWKey *key = module->getKey();
115  VerseKey *vkey = SWDYNAMIC_CAST(VerseKey, key);
116 
117  if (vkey)
118  vkey->setIntros(true);
119 
120  for ((*module) = TOP; !module->popError(); (*module)++) {
121  std::cout << "$$$" << module->getKeyText() << std::endl;
122  std::cout << ((render) ? module->renderText().c_str() : (strip) ? module->stripText() : module->getRawEntry()) << "\n";
123  }
124 
125  cout << endl;
126 
127  return 0;
128 }
129 
#define TOP
Definition: swkey.h:68
SWText * module
Definition: osis2mod.cpp:105
int main(int argc, char **argv)
Definition: addcomment.cpp:32
#define SWDYNAMIC_CAST(className, object)
Definition: defs.h:47
void usage(const char *app)
Definition: imp2gbs.cpp:65
SWMgr * mgr
Definition: installmgr.cpp:46