Normalize Rich Text Markup

Hello,

I found this script and would like to modify it to work with (embdeded OLE, bullet, indent). So far I have modified it to include bullet, indent but having issues with the embdeded OLE. Any help would be appreciated. Thank you.

-Jim
 

// Normalize Rich Text Markup
 
/*
Removes embedded font markup problems that were suppressed in DOORS v5 and seen in DOORS v6
*/
 
/*
Requirements:

(1)  Eliminate all non-standard Font Markup for Object Heading and Object Text
(2)  Preserve Rich Text Markup ( bold, italic, strikethru, underline, superscript, subscript)
(3)  Preserve Symbol Font
(4)  Do not modify Object Text with embedded OLE, bullet, or indent markup
     (new features in DOORS 6)

Note:  This function should be used after the conversion of a Module from
       DOORS 5 to DOORS 6, and not after any DOORS 6 specific Rich Text functions
       (embdeded OLE, bullet, indent) have been applied, as they are not considered in the conversion

Note:  This is similar to the DOORS 5 function <lib/dxl/standard/doctools/symbconv.dxl>, but
       the \\...0 markup termination notation not used because is not documented
*/
 
const string SF_ABOUT = "
   Copyright (c) 2003-2004    Galactic Solutions Group, LLC
        http://galactic-solutions.com
        Author: michael.sutherland@galactic-solutions.com
 
   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.
 
   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.
 
   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   or contact http://www.gnu.org
 
   If you find defects with or improve upon this library, please contact
   support@galactic-solutions.com
"
 
/*
      1.0.1  20 June 2004        Michael Sutherland - Galactic Solutions Group, LLC
       - Updated for DOORS 7.x versions
         - Updated to eliminate "deleteFontTable" option on DOORS 7
           - Updated SF_TabSelectFn() added call to makeBox()
         - Updated warnIncorrectDOORSVersion()
         - Updated checkDOORSVersion()
           - Added Skip SF_DOORS_VERSIONS
       - Updated SF_doAboutTab() to display richText()
       - Updated size of tab() in makeBox()

      1.0    10 February 2003    Michael Sutherland - Galactic Solutions Group, LLC
       - Initial release to fix post DOORS 6.0 SR1 conversion issues with Fonts
*/
 
// Print debugging info?
const bool DEBUG = false
 
const Skip SF_DOORS_VERSIONS = create
put( SF_DOORS_VERSIONS, 1, "DOORS 7.1"     )
put( SF_DOORS_VERSIONS, 2, "DOORS 7"       )
put( SF_DOORS_VERSIONS, 3, "DOORS 6.0 SR1" )
put( SF_DOORS_VERSIONS, 4, "DOORS 6"       )
put( SF_DOORS_VERSIONS, 5, "DOORS 9.3.0.0" )
put( SF_DOORS_VERSIONS, 6, "DOORS 9.3.0.1" )
put( SF_DOORS_VERSIONS, 7, "DOORS 9.3.0.2" )
put( SF_DOORS_VERSIONS, 8, "DOORS 9.3.0.3" )
put( SF_DOORS_VERSIONS, 9, "DOORS 9.3.0.4" )
put( SF_DOORS_VERSIONS, 10, "DOORS 9.3.0.5" )
put( SF_DOORS_VERSIONS, 11, "DOORS 9.3.0.6" )
put( SF_DOORS_VERSIONS, 12, "DOORS 9.3.0.7" )
 
const string SF_DOORS_VERSION = "DOORS 6.x, 7.0, 7.1, 9.3.0.x"
 
const string SF_exportMainTitle       = "Normalize Rich Text Markup"
const string SF_exportExporterVersion = "1.0.1"
const string SF_exportULTitle         = SF_exportMainTitle " (v" SF_exportExporterVersion ") "
 
const void warnIncorrectDOORSVersion( string correctVersion ) {
   if ( doorsVersion == "DOORS 7.0 SP1" ) {
      warningBox "A defect in the DOORS 7.0 SP1 Rich Text commands creates problems\n" //-
                 "normalizing Rich Text and Symbol Font (DOORS 7.0 SP1 only)\n\n" //-
                 "Galactic recommends upgrading to DOORS 7.1"
   }
   else {
      warningBox "This version (" SF_exportExporterVersion ") of the '" //-
                  SF_exportMainTitle //-
                 "' has been written for " correctVersion "\n\n" //-
                 "You are using " doorsVersion "\n\n" //-
                 "Please download the version for " doorsVersion " from http://galactic-solutions.com\n"
   }
}
 
const string SF_TabLabels[] = { "General", "About" }
const int SF_GENERAL_TAB  = 0
const int SF_ABOUT_TAB    = 1
 
const string SF_applyToLabel = "Apply to:"
const string SF_applyTo[] =  { "Current object", "Objects in current view", "All objects in module" } 
const string SF_deleteFontTabelLabel = "Delete Font Table"
const string SF_WarningMessage = "This script will edit the Object Heading and Object Text\n" //-
                                 "for Objects in this Module, and replace the existing content\n" //-
                                 "with equivalent text content and equivalent Rich Text Markup,\n" //-
                                 "eliminating non-standard character set information.\n" //-
                                 "\n" //-
                                 "Rich Text Markup (bold, italic, etc.) and Symbol character set is preserved.\n" //-
                                 "Object Text with embedded OLE Objects, bullets or indents will not be edited.\n" //-
                                 "\n" //-
                                 "If the script does not perform as expected, close the Module without saving.\n" //-
                                 "Do you wish to continue?"
 
DB SF_DB = null
DBE SF_applyToDBE
DBE SF_deleteFontTableDBE
DBE SF_TabDBE
DBE SF_aboutTextDBE
 
Skip SF_TabSkip
Skip SF_GeneralTabSkip
Skip SF_AboutTabSkip
 
 
void debug( string s ) {
   if ( DEBUG ) print s "\n"
}
 
bool checkDOORSVersion( Skip &doorsVersions ) {
   bool doorsVersionMatches = false
   string doorsSpecificVersionString = ""
   for doorsSpecificVersionString in doorsVersions do {
      if ( doorsSpecificVersionString == doorsVersion ) {
         return true
      }
   }
   return false
}
 
string trimTrailingSpaces(string src) {
  int last = length(src)-1
  while (last > 0 && isspace(src[last])) last--
  if (src[0:last]==" ") return ""
  return src[0:last]
} 
 
Skip attributesToFix = create
put( attributesToFix, 1, "Object Heading" )
put( attributesToFix, 2, "Object Text" )
 
string normalizeRichTextMarkup( string richStringOld ) {
 
   Buffer richBufferNew = create
 
   RichTextParagraph rp
   for rp in richStringOld do {
 
      RichText rt
      int rtfChunkCount = 0
   
      for rt in rp do {
         string text = rt.text
         string markupPrefix = ""
         string markupSuffix = ""
         rtfChunkCount++
         debug( "\t\tRTF Chunk: " rtfChunkCount "" )
         if ( rt.bold ) {
            markupPrefix = "{\\b " markupPrefix 
            markupSuffix = markupSuffix "}"
            debug( "\t\t\tBold" )
         }
         if ( rt.italic ) {
            markupPrefix = "{\\i " markupPrefix 
            markupSuffix = markupSuffix "}"
            debug( "\t\t\tItalic" )
         }
         if ( rt.strikethru ) {
            markupPrefix = "{\\strike " markupPrefix 
            markupSuffix = markupSuffix "}"
            debug( "\t\t\tStrikethru" )
         }
         if ( rt.underline ) {
            markupPrefix = "{\\ul " markupPrefix 
            markupSuffix = markupSuffix "}"
            debug( "\t\t\tUnderline" )
         }
         if ( rt.superscript ) {
            markupPrefix = "{\\super " markupPrefix 
            markupSuffix = markupSuffix "}"
            debug( "\t\t\tSuperscript" )
         }
         if ( rt.subscript ) {
            markupPrefix = "{\\sub " markupPrefix 
            markupSuffix = markupSuffix "}"
            debug( "\t\t\tSubscript" )
         }
   
         if ( rt.newline ) {
            markupSuffix = markupSuffix "\n"
            debug( "\t\t\tNewline" )
         }
   
         if ( rt.charset == charsetAnsi ) {
            debug( "\t\t\tCharacter Set Ansi" )
         }
      
         if ( rt.charset == charsetSymbol ) {
            markupPrefix = markupPrefix "{\\f1001 " 
            markupSuffix = "}" markupSuffix 
            debug( "\t\t\tCharacter Set Symbol " "[" text "]" )
         }
   
         // No action will be taken for the following character sets:   
         if ( rt.charset == charsetGreek ) {
            debug( "\t\t\tCharacter Set Greek" )
         }
         if ( rt.charset == charsetRussian ) {
            debug( "\t\t\tCharacter Set Russian" )
         }
         if ( rt.charset == charsetEastEurope ) {
            debug( "\t\t\tCharacter Set EastEurope" )
         }
         if ( rt.charset == charsetTurkish ) {
            debug( "\t\t\tCharacter Set Turkish" )
         }
         if ( rt.charset == charsetHebrew ) {
            debug( "\t\t\tCharacter Set Hebrew" )
         }
   
         richBufferNew += markupPrefix text markupSuffix
      }
   }
 
   string richStringNew = trimTrailingSpaces( stringOf( richBufferNew ) )
   delete richBufferNew
 
   return richStringNew
}
 
void normalizeRichTextMarkupForAttributes( Object o, Skip &attributesToFix ) {
   debug( "Object Number [" number( o ) "] Absolute Number [" o."Absolute Number" "]" )
 
   if ( canModify( o ) ) {
 
      string attributeToFix
      for attributeToFix in attributesToFix do {
 
         if ( canModify( o.attributeToFix ) ) {
 
            debug( "\tAttribute Name: " attributeToFix "" )
      
            string s = richTextWithOle o.attributeToFix
      
            bool attributeHasOLE = false
            bool attributeHasBullet = false
            bool attributeHasIndent = false
                        int indent = 0
            RichTextParagraph rp
            for rp in s do {
               if ( rp.isBullet ) attributeHasBullet = true
               if ( rp.indentLevel != 0 ) 
                           {
                                        attributeHasIndent = true 
                                        indent = rp.indentLevel
                           }
               RichText rt
               for rt in rp do {
                  if ( rt.isOle ) attributeHasOLE = true
               }
            }
      
            //if ( !attributeHasOLE && !attributeHasBullet && !attributeHasIndent ) {
                        if ( !attributeHasOLE ) {
      
               // Get Original Value with Rich Text
               string richStringOld = richText( o.attributeToFix )
      
               // Get Original Value with Rich Text stripped away
               string stringOld = o.attributeToFix
               stringOld = trimTrailingSpaces( stringOld )
        
               debug( "\t\tOld Text:" richStringOld )
      
               if ( !null stringOld ) {
      
                  // Get New Value with Rich Text
                  string richStringNew = normalizeRichTextMarkup( richStringOld )
         
                  // Get New Value with Rich Text stripped away
                  string stringNew = o.attributeToFix
                  stringNew = trimTrailingSpaces( stringNew )
                  
                  debug( "\t\tNew Text:" richStringNew )
         
                  // If old and new values (both with Rich Text stripped away)
                  // do not match, that is an error
                  if ( stringOld == stringNew ) {
                     //o.attributeToFix = richText( richStringNew )
                                         o.attributeToFix = richText(applyTextFormattingToParagraph (richStringNew, attributeHasBullet, indent, 0))
                  }
                  else {
                     string errorStr = "***ERROR - Data Mismatch for Object " o."Absolute Number" ""
                     errorBox( errorStr ) 
                     debug( errorStr )
                     debug( "[" stringOld "]" )
                     debug( "[" stringNew "]" )
                  }
               }
            }
            else {
               debug( "\tAttribute not processed: " attributeToFix "" )
               if ( attributeHasOLE )    debug ( "\t\tHas OLE" )
               if ( attributeHasBullet ) debug ( "\t\tHas Bullet" )
               if ( attributeHasIndent ) debug ( "\t\tHas Indent" )
            }
         }
         else {
            debug( "No access to modify attribute '" attributeToFix "'" )
         }
      }
   }
   else {
      debug( "No access to modify object" )
   }
}
 
void applyToCurrent( void ) {
   normalizeRichTextMarkupForAttributes( current Object, attributesToFix )
}
 
void applyToView( void ) {
   Object o
   for o in current Module do {
      normalizeRichTextMarkupForAttributes( o, attributesToFix )
   }
}
 
void applyToAll( void ) {
   filtering off
   graphics off
   linksVisible on
   outlining off
   showPictures on
   showTables on
   sorting off
   level 0
   ancestors off
   descendants off
   showDeletedObjects false
   hideExplorer( current Module )
 
   Object o
   for o in current Module do {
      normalizeRichTextMarkupForAttributes( o, attributesToFix )
   }
}   
 
void closeDB(DB db) {
    hide SF_DB
}
 
void normalizeMarkup( DB xx ) {
   if ( get( SF_deleteFontTableDBE ) ) {
      deleteFontTable( current Module )
   }
 
   showSelectedTab( SF_TabSkip, SF_ABOUT_TAB )
 
   if ( get( SF_applyToDBE ) == 0 ) applyToCurrent
   if ( get( SF_applyToDBE ) == 1 ) {
      applyToView
      infoBox SF_exportMainTitle " complete."
      release SF_DB
   }
   if ( get( SF_applyToDBE ) == 2 ) {
      applyToAll
      infoBox SF_exportMainTitle " complete."
      release SF_DB
   }
}
 
void SF_TabSelectFn( DBE theTab ) {
    int selection = get theTab
    showSelectedTab( SF_TabSkip, selection ) 
    if ( matches( "DOORS 7", doorsVersion ) ) {
       hide SF_deleteFontTableDBE
    }
}
 
void SF_doGeneralTab( DB SF_WinDB,
                      DBE SF_TabDBE,
                      Skip SF_GeneralTabSkip
                    )
{
   SF_applyToDBE = verticalRadioBox( SF_DB, SF_applyToLabel, SF_applyTo, 0 )
   SF_deleteFontTableDBE = toggle( SF_DB, SF_deleteFontTabelLabel, false )
 
   SF_applyToDBE->"top"->"inside"->SF_TabDBE
   SF_applyToDBE->"left"->"inside"->SF_TabDBE
 
   int index = 0
   put( SF_GeneralTabSkip, index++, SF_applyToDBE )
   put( SF_GeneralTabSkip, index++, SF_deleteFontTableDBE )
}
 
void SF_doAboutTab( DB SF_WinDB,
                    DBE SF_TabDBE,
                    Skip SF_AboutTabSkip
                  )
{
   SF_aboutTextDBE = richText( SF_WinDB, SF_exportULTitle, SF_ABOUT, 160, 160, true )
 
   SF_aboutTextDBE->"top"->"inside"->SF_TabDBE
   SF_aboutTextDBE->"left"->"inside"->SF_TabDBE
   SF_aboutTextDBE->"right"->"inside"->SF_TabDBE
 
   int index = 0
   put( SF_AboutTabSkip, index++, SF_aboutTextDBE )
}
 
void makeBox() {
 
   if ( null SF_DB ) {
 
      SF_DB = create( SF_exportULTitle, styleCentered|styleFixed )
   
      SF_TabSkip = create
   
      SF_TabDBE = tab( SF_DB, SF_TabLabels, 550, 400, SF_TabSelectFn )
   
      SF_GeneralTabSkip = create
      SF_doGeneralTab( SF_DB, SF_TabDBE, SF_GeneralTabSkip )
      put( SF_TabSkip, SF_GENERAL_TAB, SF_GeneralTabSkip )
   
      SF_AboutTabSkip = create
      SF_doAboutTab( SF_DB, SF_TabDBE, SF_AboutTabSkip )
      put( SF_TabSkip, SF_ABOUT_TAB, SF_AboutTabSkip )
   
      ok( SF_DB, "OK", normalizeMarkup )
      close( SF_DB, true, closeDB )
 
      showSelectedTab( SF_TabSkip, SF_GENERAL_TAB )    
   }
 
   showSelectedTab( SF_TabSkip, SF_GENERAL_TAB )    
   SF_TabSelectFn( SF_TabDBE )
   show SF_DB
}
 
if ( !checkDOORSVersion( SF_DOORS_VERSIONS ) ) {
   warnIncorrectDOORSVersion( SF_DOORS_VERSION )
}
else {
   if ( null current Module ) {
      warningBox "Please run in a Module"
   }
   else {   
      if ( null current Object ) {
         warningBox "No Objects in curent Module"
      }
      else {
         if ( confirm( SF_WarningMessage, msgWarning ) ) {
            makeBox
         }
      }
   }
}

SystemAdmin - Thu Jun 07 21:12:55 EDT 2012