home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1998 October A / Pcwk10a98.iso / Corel / Ventura8 / Ventura / Scripts / RTFFormatter.csc < prev    next >
Encoding:
Text File  |  1998-07-08  |  7.1 KB  |  210 lines

  1. REM Automatically formats the selection [CorelSCRIPT 8]
  2. REM RTFFormatter.csc  March, 1998
  3. REM ⌐ 1998 Corel Corporation. All rights reserved.
  4.  
  5. REM ***********************************************************************
  6. REM This script automatically applies formatting to the current text selection.
  7. REM If there is no selection, formatting is applied to the current text file.
  8. REM ***********************************************************************
  9.  
  10. REM ***********************************************************************
  11. REM MAIN HEADING definition
  12. REM - a paragraph with only one line
  13. REM - the next line is a paragraph return (ie. a para with width=0)
  14. REM - no punctuation at end of line (ie. period, question mark, etc)
  15. REM - no number or special character at start of line
  16. REM ***********************************************************************
  17.  
  18. REM ***********************************************************************
  19. REM MINOR HEADING definition
  20. REM - a paragraph with only one line
  21. REM - text is bold
  22. REM - no punctuation at end of line (ie. period, question mark, etc)
  23. REM - no number or special character at start of line
  24. REM ***********************************************************************
  25.  
  26. REM ***********************************************************************
  27. REM SUB HEADING definition
  28. REM - a paragraph with only one line
  29. REM - no punctuation at end of line (ie. period, question mark, etc)
  30. REM - no number or special character at start of line
  31. REM ***********************************************************************
  32.  
  33. REM ***********************************************************************
  34. REM NUMBERED LIST definition
  35. REM - first character of first line of paragraph is a number
  36. REM - paragraph can be one or more lines
  37. REM ***********************************************************************
  38.  
  39. REM ***********************************************************************
  40. REM BULLET definition
  41. REM - paragraph can be one or more lines
  42. REM - first line of paragraph is indented
  43. REM - first character of first line of paragraph is a special character (ie. not a letter or a number) 
  44. REM ***********************************************************************
  45.  
  46. REM ***********************************************************************
  47. REM BODY TEXT definition
  48. REM - anything that doesn't fall into one of the other tag definitions
  49. REM ***********************************************************************
  50.  
  51. #addfol "..\..\Scripts"        ' create a tmporary folder to provide a path for the include files
  52. #include "ScpConst.csi"        ' this enables the include files to be located 
  53. #include "VPConst.csi"        
  54.  
  55. #DEFINE SUB_HEADING        1
  56. #DEFINE MINOR_HEADING    2
  57. #DEFINE MAIN_HEADING    3
  58. #DEFINE BULLET            4
  59. #DEFINE NUMBERED_LIST    5
  60.  
  61. DECLARE FUNCTION ISPunctuated() AS BOOLEAN
  62. DECLARE FUNCTION ISTitleCase() AS BOOLEAN
  63. DECLARE FUNCTION ISMainHeading() AS BOOLEAN
  64. DECLARE FUNCTION ListType(TagScore%) AS INTEGER
  65. DECLARE SUB ApplyTag(TagType%)
  66.  
  67. WITHOBJECT "CorelVentura.Automation.8"
  68.     .PageFirstLine
  69.     WHILE NOT(.IsCaretAtEndOfText())
  70.         TagScore% = 0
  71.  
  72.         'Get current para info
  73.         .TextLineInfoGet CurrentLineCount&, LineNumber&, ColumnNumber&, LineLeft&, LineTop&, Width&, Height&, BaseLineY&
  74.  
  75.         IF CurrentLineCount& = 1 THEN 
  76.             'check for punctuation at end of line
  77.             IF ISPunctuated() = FALSE THEN TagScore% = SUB_HEADING        'tag para with sub heading
  78.             IF ISTitleCase() = TRUE THEN TagScore% = TagScore% + 1
  79.             IF ISMainHeading() = TRUE THEN TagScore% = TagScore% + 1    'tag para with main heading
  80.         ENDIF
  81.  
  82.         IF TagScore% <= 1 THEN TagScore% = TagScore% + ListType%(TagScore%)
  83.  
  84.         ApplyTag TagScore%
  85.         .TextParaDown 1, TRUE        'activate next paragraph
  86.     WEND
  87. END WITHOBJECT
  88.  
  89.  
  90. ' This function will apply the tag specified by TagType to the current paragraph
  91. SUB ApplyTag(TagType%)
  92.     WITHOBJECT "CorelVentura.Automation.8"
  93.         SELECT CASE(TagType%)
  94.             CASE SUB_HEADING
  95.                 .FormatSetParaTag "Subheading"
  96.             CASE MINOR_HEADING
  97.                 .FormatSetParaTag "Minor Heading"
  98.             CASE MAIN_HEADING
  99.                 .FormatSetParaTag "Main Heading"
  100.                 .TextParaDown 1, FALSE
  101.                 .TextParaDown 1, TRUE
  102.                 .EditDelete
  103.                 .TextParaUp 1, FALSE
  104.             CASE BULLET
  105.                 .FormatSetParaTag "Bullet"
  106.             CASE NUMBERED_LIST
  107.                 .FormatSetParaTag "Numbered List"
  108.             CASE ELSE
  109.                 .FormatSetParaTag "Body Text"
  110.         END SELECT
  111.     END WITHOBJECT
  112. END SUB
  113.  
  114. ' This function checks the current line for title case (ie. First letter of each word is uppercase
  115. FUNCTION ISTitleCase() AS BOOLEAN
  116.     WITHOBJECT "CorelVentura.Automation.8"
  117.         UpperCnt% = 0
  118.         LowerCnt% = 0
  119.         
  120.         .TextStartOfLine
  121.         DO
  122.             .TextWordRight 1, FALSE    'select word
  123.             CurrentText$ = .SelectedText()
  124.             SELECT CASE ASC(CurrentText$)
  125.                 CASE 65 TO 90    ' word begins with an uppercase letter
  126.                     UpperCnt% = UpperCnt% + 1    
  127.                 CASE ELSE
  128.                     LowerCnt% = LowerCnt% + 1    
  129.             END SELECT
  130.         LOOP UNTIL ASC(CurrentText$) = 0    
  131.         
  132.         IF UpperCnt% > LowerCnt% THEN 
  133.             ISTitleCase = TRUE
  134.         ELSE
  135.             ISTitleCase = FALSE
  136.         ENDIF
  137.     
  138.     END WITHOBJECT
  139. END FUNCTION
  140.  
  141.  
  142. ' This function checks the current line for punctuation at the end of the line
  143. FUNCTION ISPunctuated() AS BOOLEAN
  144.     WITHOBJECT "CorelVentura.Automation.8"
  145.         .TextEndOfLine FALSE
  146.         .TextWordLeft 1, TRUE
  147.         LastChar& = ASC(.SelectedText())
  148.  
  149.         IF LastChar& = 33 OR LastChar& = 46 OR LastChar& = 63 THEN
  150.             ISPunctuated = TRUE
  151.         ELSE
  152.             ISPunctuated = FALSE
  153.         ENDIF
  154.         .TextStartOfLine FALSE
  155.     END WITHOBJECT
  156. END FUNCTION
  157.  
  158. ' This function determines whether to apply the Main Heading tag to the current line
  159. ' If the current line is a Main Heading, the function returns TRUE
  160. ' The current line is a Main Heading IF the next line is a para return
  161. FUNCTION ISMainHeading() AS BOOLEAN
  162.     WITHOBJECT "CorelVentura.Automation.8"
  163.         ' is the next line a para return
  164.         .TextParaDown 1, FALSE
  165.         .TextLineInfoGet NextLineCount&, LineNumber&, ColumnNumber&, LineLeft&, LineTop&, NextWidth&, Height&, BaseLineY&
  166.         IF NextWidth& = 0  THEN 
  167.             ISMainHeading = TRUE
  168.             .TextParaUp 1, FALSE
  169.             EXIT FUNCTION
  170.         ENDIF
  171.         .TextParaUp 1, FALSE
  172.         ISMainHeading = FALSE
  173.     END WITHOBJECT
  174. END FUNCTION
  175.  
  176.  
  177. ' This function determines whether to apply the numbered List tag to the current line
  178. ' If the current line is a numbered List, the function returns TRUE
  179. ' The current line is a numbered List IF the first character of the current line is a number
  180. FUNCTION ListType(TagScore%) AS INTEGER
  181.     WITHOBJECT "CorelVentura.Automation.8"
  182.          ' is first character a number
  183.         .TextStartOfLine
  184.         TextType& = .TextSpecialItemType(CharCode&)
  185.         DO WHILE TextType& <> 0
  186.             .TextCharRight 1
  187.             TextType& = .TextSpecialItemType(CharCode&)
  188.         LOOP 
  189.  
  190.         SELECT CASE CharCode& 
  191.             CASE 48 TO 57        ' a number from 0 to 9
  192.                 ListType = NUMBERED_LIST - TagScore%
  193.             CASE 65 TO 90        ' upper case letters
  194.                 ListType = 0
  195.             CASE 97 TO 122        ' lower case letters
  196.                 ListType = 0
  197.             CASE 183            ' bullet char
  198.                 .TextCharRight 1, TRUE
  199.                 .EditDelete
  200.                 ListType = BULLET - TagScore%
  201.             CASE 9            ' tab
  202.                 ListType = BULLET - TagScore%
  203.             CASE 32            ' space
  204.                 ListType = BULLET - TagScore%
  205.             CASE ELSE
  206.                 ListType = 0
  207.         END SELECT
  208.     END WITHOBJECT
  209. END FUNCTION
  210.