示例#1
0
ustring stylesheet_import(const ustring & filename)
// Imports a new stylesheet from "filename".
// It expects a file in the format as it is given in the export function.
{
  // See that the sheet is a recognized one.
  // Derive the name of the new stylesheet from the filename.
  ustring name;
  for (unsigned int i = 0; i < (sizeof(RECOGNIZED_SUFFIXES) / sizeof(*RECOGNIZED_SUFFIXES)); i++) {
    if (g_str_has_suffix(filename.c_str(), RECOGNIZED_SUFFIXES[i])) {
      name = gw_path_get_basename(filename);
      name.erase(name.length() - strlen(RECOGNIZED_SUFFIXES[i]), strlen(RECOGNIZED_SUFFIXES[i]));
    }
  }
  if (name.empty()) {
    gtkw_dialog_error(NULL, _("Unrecognized stylesheet: ") + filename);
    return "";
  }
  // Check whether it already exists.
  if (stylesheet_exists(name)) {
    gtkw_dialog_error(NULL, _("Stylesheet already exists: ") + name);
    return "";
  }
  // Get the path of the new stylesheet.
  ustring path = stylesheet_xml_filename(name);
  // Copy the file.
  unix_cp(filename, path);
  // Upgrade the stylesheet.
  stylesheets_upgrade();
  // Return the name of the stylesheet we imported;
  return name;
}
示例#2
0
void stylesheet_export_bibledit(const ustring & name, const ustring & filename)
// Exports a stylesheet in standard bibledit format.
{
  ustring originalfile = stylesheet_xml_filename(name);
  ustring destinationfile(filename);
  destinationfile.append(STYLESHEET_XML_SUFFIX);
  unix_cp(originalfile, destinationfile);
}
示例#3
0
void stylesheet_create_new(const ustring & name, StylesheetType stylesheettype)
// Create a new stylesheet, named "name", from the template.
{
  // Copy the template database.
  ustring templatefile = stylesheet_xml_template_filename();
  ustring stylefile = stylesheet_xml_filename (name);
  unix_cp(templatefile, stylefile);
 
  // Take action depending on the type of stylesheet we're making.
  if (stylesheettype == stFull)
    return;
  set <ustring> desired_markers = stylesheet_get_styles_of_type(stylesheettype);
  // Take the new stylesheet, and remove the unwanted markers.
  vector <ustring> all_markers = stylesheet_get_markers(name, NULL);
  vector <ustring> undesired_markers;
  for (unsigned int i = 0; i < all_markers.size(); i++) {
    if (desired_markers.find(all_markers[i]) == desired_markers.end())
      undesired_markers.push_back(all_markers[i]);
  }
  for (unsigned int i = 0; i < undesired_markers.size(); i++) {
    stylesheet_delete_style(name, undesired_markers[i]);
  }
}
示例#4
0
ustring script_filter(const ustring & scriptname, bool straightthrough, const ustring & inputfile, const ustring & outputfile)
/*
 Runs the filter "scriptname".
 Input text in "inputfile", and the output text goes in "outputfile".
 If everything is okay, it returns nothing.
 If there were errors, it returns these.
 */
{
  // Remove any previous output.
  unlink(outputfile.c_str());
  unlink(script_temporal_error_file().c_str());

  // Handle straight through.
  if (straightthrough) {
    unix_cp(inputfile, outputfile);
    return "";
  }
  // Get the filename and the type of the script.
  ScriptType scripttype;
  ustring scriptfile = script_get_path(scriptname, &scripttype, true);

  // If the rules file does not exist, or the script is of an unknown type, pass it straight through.
  if (!g_file_test(scriptfile.c_str(), G_FILE_TEST_IS_REGULAR) || (scripttype == stEnd)) {
    unix_cp(inputfile, outputfile);
    gw_warning(_("Error in script ") + scriptname);
    return "";
  }
  // Encode the input usfm file.
  ustring encodedinputfile = script_temporal_input_file();
  if (inputfile != encodedinputfile) {
    unix_cp(inputfile, encodedinputfile);
  }
  script_encode_usfm_file(encodedinputfile);

  // Run filter.
  ustring command;
  ustring error;
  switch (scripttype) {
  case stSed:
    {
      command.append(script_sed_binary());
      command.append(" -f");
      command.append(shell_quote_space(scriptfile));
      command.append("<");
      command.append(shell_quote_space(encodedinputfile));
      command.append(">");
      command.append(shell_quote_space(outputfile));
      break;
    }
  case stTECkit:
    {
      command.append(script_teckit_txtconverter());
      command.append(" -i");
      command.append(shell_quote_space(encodedinputfile));
      command.append(" -o");
      command.append(shell_quote_space(outputfile));
      command.append(" -t");
      command.append(shell_quote_space(scriptfile));
      command.append(" -nobom");
      break;
    }
  case stFree:
    {
      // Text of the script.
      ustring scriptdata;
      {
        // Read script.
        gchar *contents;
        g_file_get_contents(scriptfile.c_str(), &contents, NULL, NULL);
        if (contents) {
          scriptdata = contents;
          g_free(contents);
        } else {
          error = _("Can't read script file");
          gw_warning(error);
          return error;
        }
      }
      // Check for and insert the input filename.
      if (scriptdata.find(script_free_input_identifier()) == string::npos) {
        error = _("Can't find where to put input file");
        gw_warning(error);
        return error;
      }
      replace_text(scriptdata, script_free_input_identifier(), shell_quote_space(encodedinputfile));
      // Check for and insert the output filename.
      if (scriptdata.find(script_free_output_identifier()) == string::npos) {
        error = _("Can't find where to put output file");
        gw_warning(error);
        return error;
      }
      replace_text(scriptdata, script_free_output_identifier(), shell_quote_space(outputfile));
      // Write temporal script.
      g_file_set_contents(script_temporal_script_file().c_str(), scriptdata.c_str(), -1, NULL);
      // Assemble command to run.
      command.append("sh");
      command.append(shell_quote_space(script_temporal_script_file()));
      break;
    }
  case stEnd:
    {
      break;
    }
  }

  // Add the error file to the command, and run it.
  command.append(" 2> ");
  command.append(script_temporal_error_file());
  int result = system(command.c_str()); // This one is too unpredictable to be used with GwSpawn.

  // The filters are so much beyond any control that we never can be sure that 
  // their output is in the UTF-8 encoding.
  // Sed would give UTF-8, but as TECkit can also give legacy encodings.
  // We can't know what free scripts will do, it could be anything.
  // So here check the UTF-8 encoding. 
  // If UTF-8 validation fails, we copy the input straight to the output.
  {
    gchar *contents;
    g_file_get_contents(outputfile.c_str(), &contents, NULL, NULL);
    if (contents) {
      if (!g_utf8_validate(contents, -1, NULL)) {
        unix_cp(inputfile, outputfile);
        error = _("UTF-8 validation failure");
        gw_warning(error);
      }
      g_free(contents);
      if (!error.empty())
        return error;
    }
  }

  // Decode the output file.
  script_decode_usfm_file(outputfile);

  // Handle OK.
  if (result == 0)
    return "";

  // Handle error.
  gchar *contents;
  g_file_get_contents(script_temporal_error_file().c_str(), &contents, NULL, NULL);
  if (contents) {
    error = contents;
    g_free(contents);
    gw_warning(error);
  }
  return error;
}
示例#5
0
void stylesheet_copy(const ustring & from_name, const ustring & to_name)
// Copies one stylesheet to another.
{
  unix_cp(stylesheet_xml_filename(from_name), stylesheet_xml_filename(to_name));
}