• Solutions
    • FERC XBRL Reporting
    • FDTA Financial Reporting
    • SEC Compliance
    • Windows Clipboard Management
    • Legato Scripting
  • Products
    • GoFiler Suite
    • XBRLworks
    • SEC Exhibit Explorer
    • SEC Extractor
    • Clipboard Scout
    • Legato
  • Education
    • Training
    • SEC and EDGAR Compliance
    • Legato Developers
  • Blog
  • Support
  • Skip to blog entries
  • Skip to archive page
  • Skip to right sidebar

Friday, September 22. 2017

LDC #53: Translate Post Process

If you’re interested in automating certain aspects of document conversion, then look no further. The TranslateSetPostProcessHook function in Legato is what you need. Added in GoFiler 4.20a, this function allows any direct import or insert from a file action to be post processed by a custom script.


What are the uses of such a hook? Maybe adding control information to the file or perhaps to manage image names. The possibilities are endless.


How the Translate Process Works


Various Novaworks applications allow for the translation of data from applications such as Microsoft Word or Excel. The process is invoked by the user by opening a file, importing to a new file, or importing into an existing file. During each of these actions the application’s translate module comes into play to aid in the selection of an appropriate conversion method, options, and destination. Setting a post process hook allows the data to be intercepted prior to being inserted in to the destination window or file.


When the hook is run, it receives the parameters: source, destination, what method was used, and any related options.  If our hook is not interested in the file, it can just ignore the operation. For example, let’s say that we want to post process an HTML conversion but the user instead runs Word to text conversion. The hook can simply exit and do nothing.


Setting the Hook


To set the hook, the TranslateSetPostProcessHook function is used. This can be called anywhere but would generally be set when the application starts up. The function parameters are as follows:


int = TranslateSetPostProcessHook ( string script, string function );


The script parameter is the name of the script to run. If the string is empty, the name and location of the currently executing script is used. The function parameter specifies the function to run after conversion has completed.


The Hook


After a conversion process has completed, the hook is called before the data is placed into the destination window or file. The hook takes the following form:


int post_process_hook (string xl_type,
                           string xl_src, string xl_dst, string xl_dst_base,
                           dword xl_flags, string xl_options );


The xl_type parameter will contain the name of the conversion method used. For example, the value could be ‘DOCtoHTML’ for a conversion from Microsoft Word. Based on this parameter, the post process can run different operations or simply ignore the conversion completely.


The xl_src parameter is the name of the source file. This is provided for reference or if the script wants to examine the content. Normally, data is post processed using the destination file.


The xl_dst parameter is the name of the destination file. This is the file that the post process script modifies.


The xl_dst_base parameter specifies the location of the destination. Depending on the context, the destination file may be located in the Windows temp folder. This path can be used reference images and other resources.


Flags are provided as a dword in the xl_flags parameter to direct operation. For example, if the XL_QUIET bit is set, then the script should not interact with the user.


Finally, the xl_options string contains ‘property: value’ pairs of information for settings.


The parameter names can be named at the programmer’s convenience but the datatype must match. When the script completes its processing, it should return ERROR_NONE. Returning an error will cause the import process to terminate.


General Operation


For HTML files, as a programmer you can either use the SGML mini parse functions or the SGML Object. The latter, while more complex, certainly allows for more options for processing. For example, an object can be created using the SGMLCreate function, by attaching a file by using the SGMLSetFile function, or by attaching a Mapped Text Object. The file can be scanned, changes can be made, and then by using the SGMLWriteTag function, the file can be updated. At the end of the process, the destination file can be saved and the importing will complete upon exiting the hook.


Here is an example to add information to the log and inject data into a “TexttoHTML” conversion:


int main() {

    int                 rc;

    rc = TranslateSetPostProcessHook("", "post_translate");
    if (IsError(rc)) {
      MessageBox('x', "Error %08X setting the hook.", rc);
      return rc;
      }
    MessageBox('i', "Hookset for 'TexttoHTML'");
    return ERROR_NONE;
    }
    
    
int post_translate(string type, string fnSource, string fnFile, 
                   string fnPath, dword flags, string options) {

    string              s1;
    int                 rc,
                        cx;
                        
    AddMessage("Add Some Stuff to the Log:");
    LogIndent();
    LogSetMessageType(LOG_ERROR);
    AddMessage("Force a log error to for the log to display");
    LogSetMessageType(LOG_INFO);
    AddMessage("Type: %s", type);
    AddMessage("fnSource: %s", fnSource);
    AddMessage("fnFile: %s", fnFile);
    AddMessage("fnPath: %s", fnPath);
    AddMessage("flags: %08X", flags);
    AddMessage("Options: %s", options);
    
    if (type == "TexttoHTML") {
      s1 = FileToString(fnFile);
      if (s1 == "") {
        AddMessage("Error processing data: %08X (opening file)", GetLastError());
        return ERROR_NONE;
        }
      cx = FindInString(s1, "<BODY>");
      if (cx < 0) { cx = 0; }
      else { cx += 6; }
      s1 = InsertStringSegment(s1, "\r\n\r\n<P STYLE='text-align: center; color: red'><B>POST PROCESS SUCCESS</B></P>", cx);
      rc = StringToFile(s1, fnFile);
      if (IsError(rc)) {
        AddMessage("Error writing data: %08X (saving file)", rc);
        return ERROR_NONE;
        }
      }
    return ERROR_NONE;
    }

In the above case, once the script is run from the IDE or as part of startup, the translate operation becomes hooked. Once that’s accomplished, the post_translate function will be run at the end of every translate operation. This script adds some information to the log before checking the filter. On a match, the function injects some HTML into the file just to illustrate how the destination can be modified.


As a side note, if you copy and paste the above script into the Legato IDE, you must save it to allow the hook to be accessed. As with all hooks the program requires the script file to be named. Otherwise, the TranslateSetPostProcessHook function will fail.


On importing a text file, select the “TexttoHTML” destination:


A screenshot of the GoFiler import dialog. 


When the translation operation is completed, the file will have the following added:


A screenshot of the resulting file showing that the post-process script ran successfully.


With log entries as follows:


A screenshot of the log file created in the GoFiler Information View by the script. 


Basically, the hook has full control over the contents of the destination file. The program can use the SGML parser to make HTML changes, contact external resources, or essentially run any file level function. Since the file is not yet a window, edit-level menu type functions cannot be run by functions such as RunMenuFunction.


Finally


Your post process script can also have options implemented using the settings functions. User access can be added using an extension tool via the MenuAddFunction function.


Here are some additional ideas for useful post-process scripting:


— Processing image names and renaming them to match a specific project or job number.

— Adding document tracking information.

— Running functions on tables, list, fields, drop caps, etc...


Keep in mind that as a programmer, you also can define and add new translate modules as you see fit using the TranslateAddHook function. While this is the ultimate power in conversion, it requires you to do all the heavy lifting. On the other hand, the post process translate hook gives you the opportunity to customize code and control the processing of newly converted data by building on existing translation functions.


If you have any questions, feel free to contact technical support.


 


Scott Theis is the President of Novaworks and the principal developer of the Legato scripting language. He has extensive expertise with EDGAR, HTML, XBRL, and other programming languages.

Additional Resources

Novaworks’ Legato Resources

Legato Script Developers LinkedIn Group

Primer: An Introduction to Legato 



Posted by
Scott Theis
in Development at 17:10
Trackbacks
Trackback specific URI for this entry

No Trackbacks

Comments
Display comments as (Linear | Threaded)
No comments
The author does not allow comments to this entry

Quicksearch

Categories

  • XML Accounting
  • XML AICPA News
  • XML FASB News
  • XML GASB News
  • XML IASB News
  • XML Development
  • XML Events
  • XML FERC
  • XML eForms News
  • XML FERC Filing Help
  • XML Filing Technology
  • XML Information Technology
  • XML Investor Education
  • XML MSRB
  • XML EMMA News
  • XML FDTA
  • XML MSRB Filing Help
  • XML Novaworks News
  • XML GoFiler Online Updates
  • XML GoFiler Updates
  • XML XBRLworks Updates
  • XML SEC
  • XML Corporation Finance
  • XML DERA
  • XML EDGAR News
  • XML Investment Management
  • XML SEC Filing Help
  • XML XBRL
  • XML Data Quality Committee
  • XML GRIP Taxonomy
  • XML IFRS Taxonomy
  • XML US GAAP Taxonomy

Calendar

Back May '25 Forward
Mo Tu We Th Fr Sa Su
Thursday, May 15. 2025
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31  

Feeds

  • XML
Sign Up Now
Get SEC news articles and blog posts delivered monthly to your inbox!
Based on the s9y Bulletproof template framework

Compliance

  • FERC
  • EDGAR
  • EMMA

Software

  • GoFiler Suite
  • SEC Exhibit Explorer
  • SEC Extractor
  • XBRLworks
  • Legato Scripting

Company

  • About Novaworks
  • News
  • Site Map
  • Support

Follow Us:

  • LinkedIn
  • YouTube
  • RSS
  • Newsletter
  • © 2024 Novaworks, LLC
  • Privacy
  • Terms of Use
  • Trademarks and Patents
  • Contact Us