Пример #1
0
int main(int argc, char **argv)
{
  QCoreApplication app(argc, argv);

  ArgumentParser parser;
  QTextStream out(stdout);

  parser.next();    // eat the program name
  if (!parser.hasNext()) {
    showHelp(out);
    return 0;
  }

  try {
    Engine eng;

    commandWebExport(out, parser);
  }
  catch (ArgumentParser::HelpException &) {
    showHelp(out);
    return 0;
  }
  catch (ArgumentParser::Exception &e){
    showError(out, e);
    return 1;
  }

  return 0;
}
Пример #2
0
static void commandWebExport(QTextStream &out, ArgumentParser &parser)
{
  std::vector<QString> ops;
  QString outputdir, title;
  bool build_file_tree = false;
  bool verbose_output = false;

  // parse the command line
  while (parser.hasNext()) {
    bool isswitch;
    QString cmd(parser.next(&isswitch));

    if (cmd == "-o")
      outputdir = parser.nextParam("-o");
    else if (cmd == "-t")
      title = parser.nextParam("-t");
    else if (cmd == "-f")
      build_file_tree = true;
    else if (cmd == "-v")
      verbose_output = true;
    else if (isswitch)
      throw ArgumentParser::ErrorException("Unknown switch: " + cmd);
    else
      ops.push_back(cmd);
  }

  if (outputdir.isEmpty())
    throw ArgumentParser::ErrorException("The output (-o) directory must be specified");

  if (!QFileInfo(outputdir).isDir())
    throw ArgumentParser::ErrorException(outputdir + " must be a directory");

  // parse the ops into the webexporter

  WebExport exporter(outputdir, out);
  QString fullcur, dir_prefix;
  int j, maxj;
  enum {
    flat_type,
    subdir_type,
    pivot_type,
  };
  short curtype = flat_type;
  QString pivot_tag, pivot_path, webpath;
  std::shared_ptr<hydra::Token> query_tok, pivot_tok;
  int scanned_count = 0;

  parseQueryTokens("", query_tok);   // init it

  if (!title.isEmpty())
    exporter.setTitle(title);

  maxj = static_cast<int>(ops.size());
  j = 0;
  while (j<maxj) {
    QString &param = ops[j];

    if (param == "flat") {
      curtype = flat_type;
    } else if (param == "subdir") {
      curtype = subdir_type;
    } else if (param == "pivot") {
      j++;
      if (j<maxj) {
        curtype = pivot_type;
        pivot_tag = ops[j];
        parseQueryTokens(pivot_tag + "*", pivot_tok);
      } else {
        out << "passing empty tag parameter to pivot subcommand!" << endl;
        curtype = flat_type;    // fail
      }
    } else if (param == "webpath") {
      j++;
      if (j<maxj)
        webpath = ops[j];
    } else if (param == "query") {
      j++;
      if (j<maxj)
        parseQueryTokens(ops[j], query_tok);
    } else {
      // must be a file param
      FileIterator ii(param);
      FileItemRecord item;
      FilePathRecord path;

      dir_prefix = prepPrefix(param);

      while (ii.hasNext()) {
        fullcur = ii.next();

        scanned_count++;
        if (verbose_output && scanned_count % 100 == 0)
          out << "Files scanned: " << scanned_count << endl;
        // load and check query
        if (Engine::instance()->getFileItem(fullcur, &item, 0, &path) != Engine::Load_OK)
          continue;
        if (!query_tok->isMatch(item.tags))
          continue;

        //std::cerr << "prefix=" << dir_prefix.utf8_str() << " cur=" << fullcur.utf8_str() << endl;
        switch (curtype) {
          case subdir_type:
            exporter.addFile(fullcur, AppendPath(webpath, JustPath(stripPrefix(fullcur, dir_prefix))), item, path.hash);
            break;
          case pivot_type:
            if (!PivotPath(pivot_tag, item.tags, pivot_path))
              continue;
            exporter.addFile(fullcur, pivot_path, item, path.hash);
            break;
          // assume flat
          default:
            exporter.addFile(fullcur, webpath, item, path.hash);
        }
      }//while
    }// if-else chain
    j++;
  }//while j

  // perform the export
  if (build_file_tree) {
    int count = exporter.commitFileCopy();

    if (count == -1)
      throw ArgumentParser::ErrorException("failed to copy files");

    out << count << " files copied" << endl;
  } else {
    int count = exporter.commitWebSite();

    if (count == -1)
      throw ArgumentParser::ErrorException("failed to build site");

    out << count << " files processed for the web site" << endl;
  }
}