home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 2: PC / frozenfish_august_1995.bin / bbs / d09xx / d0925.lha / DonsGenies / DonsGenies.lha / Don'sGenies / StyleTagsAutoCreate.pprx < prev    next >
Text File  |  1993-05-25  |  23KB  |  567 lines

  1. /* This genie creates new style tags automatically by examining all the text in a document. If any text is in a style which does not match an existing tag, a new tag is created and the text is set to that tag. Tags are named with a simplified description of the typeface; you can alter any of these names to anything you wish (using the "Style Tags Modify" menu item). If you modify the tag, its typeface name might no longer suit it. 
  2. Don Cox ⌐ Sept 92  Not Public Domain. All rights reserved. */
  3.  
  4. /* Method: load in typographic data from article texts. If an existing style is used, see if it is modified (further data after the style name). Ignore any data for text after the first letter. If no existing style is used, assemble the data and compare with all existing styles to see if there is a match. If not, suggest a name for a new style, create it and apply it to the block. */
  5.  
  6.  
  7. trace n
  8. if ~show(l, "gdarexxsupport.library") then
  9.     if ~(exists("libs:gdarexxsupport.library") & addlib("gdarexxsupport.library", 0, -30)) then
  10.         exit_msg("Please install the gdarexxsupport.library in your libs: directory before running this Genie")
  11.     
  12.  
  13. address command
  14. call SafeEndEdit.rexx()
  15. call ppm_AutoUpdate(0)
  16.  
  17. prevdoc  = ppm_GetDocName()
  18.  
  19. if ppm_DocChanged() then
  20. do
  21.     if ppm_SavedDate() = "Not Saved" then prevdoc = ""
  22.  
  23.     if ppm_Inform(2, "You must save the document first. Save and continue?", "Cancel", "Ok") then
  24.                 call ppm_SaveDocument(prevdoc)
  25.     else
  26.             exit_msg(0,)
  27.  
  28.     prevdoc = ppm_GetDocName()
  29. end
  30.  
  31. colormode = ppm_GetColorMode()
  32. call ppm_SetColorMode(0)
  33. cpage   = ppm_CurrentPage()
  34.  
  35.  
  36. tolerant = ppm_Inform(2,"Ignore bold, italic and underline?","No","Yes")
  37. call ppm_ShowStatus("Working..")
  38.  
  39. existing = 0   /* flag for style matching existing style  */
  40. randval = (randu() * time(s)) % 1 /* mark boxes with random number to avoid doing them twice */
  41. box = ppm_DocFirstBox()
  42.  
  43. do while box ~= 0
  44.  
  45.     info    = upper(word(ppm_GetBoxInfo(box), 1))
  46.  
  47.     if (info = "TEXT") & (ppm_GetBoxUserData(box) ~= randval) then do
  48.         oldbox = box
  49.         box = ppm_ArtFirstBox(box)
  50.         text = ppm_GetArticleText(box, 1)
  51.         call ppm_ShowStatus("  Analysing article that begins in box "box)
  52.         if text  = '' then iterate
  53.  
  54.         paraname = ""
  55.         paracode = ""
  56.         stylename = ""
  57.         stylecode = ""
  58.         fontname = ""
  59.         typestyle = ""
  60.         fontsize = ""
  61.         bold = ""
  62.         italic = ""
  63.         underline = ""
  64.         outline = ""
  65.         kerning = ""
  66.         hyphenation = ""
  67.         linespace = ""
  68.         linespacecode = ""
  69.         lineshift = ""
  70.         tracking = ""
  71.         colour = ""
  72.         justification = ""
  73.  
  74. position = 1
  75. trace n
  76.         do x = 1 to 10000  /* big number - go right through article  */
  77.             change = 0
  78.             notcode = 0
  79.             position = parsecodes(position) /* parse a block of codes in text */
  80.             if position = 0 then break
  81.             if stylecode = "dS" then iterate
  82.             if notcode = 1 then iterate   /* non-style codes  */
  83.  
  84. /* put together style definition from text */
  85.             trackstring = "\t<"tracking">"
  86.             if tracking = "" then trackstring = ""
  87.             linespacestring = "\"linespacecode"<"||linespace||">"
  88.             if linespacecode = "" | linespacecode = "" then linespacestring = ""
  89.             fontnamestring = "\ff<"fontname">"
  90.             if fontname = "" then fontnamestring = ""
  91.             fontsizestring = "\fs<"fontsize">"
  92.             if fontsize = "" then fontsizestring = ""
  93.             colourstring = "\c<"colour">"
  94.             if colour = "" then colourstring = ""
  95.             styledefinition = "\"paracode||paraname ||typestyle ||bold ||italic ||outline ||underline || fontnamestring|| fontsizestring|| justification|| kerning|| hyphenation|| linespacestring|| trackstring||colourstring
  96.             
  97.             if tolerant = 1 then styledefinition = "\"paracode||paraname || fontnamestring|| fontsizestring|| justification|| kerning|| hyphenation|| linespacestring|| trackstring||colourstring
  98.             
  99. /* see if new definition matches any of the old ones */
  100.             stylelist = ppm_GetStyleTagList()
  101.             stylelistTest = stylelist||"0a"x
  102.             parse var stylelist NumberOfTags "0a"x stylelist
  103.  
  104.             if NumberOfTags~=0 then do
  105.                 do t=1 to NumberOfTags
  106.                     parse var stylelist thisname "0a"x stylelist
  107.                     thisdata = ppm_GetStyleTagData(thisname)
  108.                     thisdata = substr(thisdata, pos("{",thisdata)+1)
  109.                     thisdata = left(thisdata, lastpos("}",thisdata)-1)
  110.                     if tolerant = 1 then do  /* take out unwanted codes */
  111.                         p=pos(thisdata,"\B")
  112.                         if p~=0 then thisdata = delstr(thisdata,p,2)
  113.                         p=pos(thisdata,"\b")
  114.                         if p~=0 then thisdata = delstr(thisdata,p,2)
  115.                         p=pos(thisdata,"\U")
  116.                         if p~=0 then thisdata = delstr(thisdata,p,2)
  117.                         p=pos(thisdata,"\u")
  118.                         if p~=0 then thisdata = delstr(thisdata,p,2)
  119.                         p=pos(thisdata,"\I")
  120.                         if p~=0 then thisdata = delstr(thisdata,p,2)
  121.                         p=pos(thisdata,"\i")
  122.                         if p~=0 then thisdata = delstr(thisdata,p,2)
  123.                         end
  124.                     if thisdata = styledefinition then do
  125.                         ThisTagName = "\dS<"||thisname||">"
  126.                         if stylecode~= "dS" then text = insert(ThisTagName,text, position-2)
  127.                         change = 0 
  128.                         leave t
  129.                         end
  130.                     end   /* t=1 to NumberOfTags  */
  131.             end
  132.      
  133.             if change = 0 then iterate x   /* No need to make a new tag */
  134.  
  135.             
  136.             numbering = 1
  137.             newbold = ""
  138.             if right(bold,1) = "B" then newbold = "B"
  139.             newitalic = ""
  140.             if right(italic,1) = "I" then newitalic = "I"
  141.             suggestname = left(fontname, 9)||newbold||newitalic||"."||(fontsize%1)||"pt."||colour
  142.             testsuggest = "0a"x||suggestname||"0a"x
  143.     
  144.             do i = 1 to 999  /* if name already used, give it a new number */
  145.                 if pos(testsuggest,stylelistTest)=0 then break
  146.                 numbering = numbering+1
  147.                 suggestname = left(fontname, 9)||newbold||newitalic||"."||(fontsize%1)||"pt."||colour"."||right(numbering, 3,"0")
  148.                 testsuggest = "0a"x||suggestname||"0a"x
  149.                 end
  150.        
  151.             ThisTagName = "\dS<"||suggestname||">"
  152.             text = insert(ThisTagName,text, position-2)
  153.             position = position+3
  154.             styledefinition = "<"suggestname"{"styledefinition"}>"
  155.             call ppm_DefineStyleTag(styledefinition)      
  156.             end   /* of article text - position = 0  */
  157.       
  158. /* replace text with new version containing new style codes */
  159.         gone = ppm_DeleteContents(box)
  160.         overflow = ppm_TextIntoBox(box, text)
  161.         do while box ~= 0  /* mark all the other boxes in this chain  */
  162.             call ppm_SetBoxUserData(box, randval)
  163.             box = ppm_ArtNextBox(box)
  164.             end
  165.         box = oldbox  /* back to the box we are working on */
  166.         end
  167.  
  168.     box = ppm_DocNextBox(box)
  169.  
  170. end
  171.  
  172.  
  173. title = ppm_GetDocName()
  174. colon = lastpos('/', title)
  175. if colon = 0 then colon = pos(':', title)
  176. title = substr(title,colon+1)
  177.  
  178. datafile = "ram:"||title||".tags"
  179. stylelist = ppm_GetStyleTagList()
  180. parse var stylelist NumberOfTags "0a"x stylelist
  181. text ="List of style tags"||"0a0a"x
  182.  
  183. if NumberOfTags~=0 then do
  184.     do t=1 to NumberOfTags
  185.         parse var stylelist thisname "0a"x stylelist
  186.         thisdata = ppm_GetStyleTagData(thisname)
  187.         text = text||thisdata"0a"x
  188.         end   /* t=1 to NumberOfTags  */
  189.     end
  190.  
  191. paralist = ppm_GetParaTagList()
  192. parse var paralist NumberOfTags "0a"x paralist
  193. text =text||"0a"x||"List of paragraph tags"||"0a0a"x
  194.  
  195. if NumberOfTags~=0 then do
  196.     do t=1 to NumberOfTags
  197.         parse var paralist thisname "0a"x paralist
  198.         thisdata = ppm_GetParaTagData(thisname)
  199.         text = text||thisdata"0a"x
  200.         end   /* t=1 to NumberOfTags  */
  201.     end
  202.  
  203. call ppm_SaveText(datafile,text)
  204. newpage = ppm_GoToPage(cpage)
  205. call exit_msg()
  206. end
  207.  
  208. /*  ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++  */
  209.  
  210. /* Parse a block of style codes in text; stop at first text character  */
  211.  
  212. parsecodes:
  213. parse arg position
  214. position = pos("\",text,position)
  215. if position = 0 then return position
  216. if substr(text,position+1,1) = "." then position = 0 /* end of text */
  217. if position = 0 then return position
  218. nonstylecode = ""
  219. stylecode = "ds"  /* assume new block of codes means new style */
  220.  
  221.         do forever
  222.             if substr(text,position,1)~="\" then break
  223.             if substr(text,position+1,1) = "." then position = 0
  224.             if position = 0 then break
  225.             code = substr(text,position+1,2)
  226. trace n
  227.             select
  228.                 when verify(left(code,1),"NMPTs-!?#","m")~=0 then do
  229.                     position = position+2 /* non-style codes */
  230.                     change = 0
  231.                     notcode = 1
  232.                     code = substr(text,position-1,5)
  233.                     select
  234.                         when left(code,4) = "#<Pp" then do
  235.                             nonstylecode = "previousboxpage"||substr(code,5,1)
  236.                             position = position+5
  237.                             end
  238.                         when left(code,4) = "#<Pc" then do
  239.                             nonstylecode = "currentpage"||substr(code,5,1)
  240.                             position = position+5
  241.                             end
  242.                         when left(code,4) = "#<Pn" then do
  243.                             nonstylecode = "nextboxpage"||substr(code,5,1)
  244.                             position = position+5
  245.                             end
  246.                         when left(code,4) = "#<Dc" then do
  247.                             nonstylecode = "creationdate"||substr(code,5,1)
  248.                             position = position+5
  249.                             end
  250.                         when left(code,4) = "#<Dp" then do
  251.                             nonstylecode = "printingdate"||substr(code,5,1)
  252.                             position = position+5
  253.                             end
  254.                         when left(code,1) = "?" then do
  255.                             nonstylecode = "comment"
  256.                             position = pos(">",text,position)
  257.                             end
  258.                         when left(code,1) = "!" then do
  259.                             nonstylecode = "boxbreak"
  260.                             end
  261.                         when left(code,1) = "P" then do
  262.                             nonstylecode = "newparagraph"
  263.                             end
  264.                         when left(code,1) = "M" then do
  265.                             nonstylecode = "mspace"
  266.                             end
  267.                         when left(code,1) = "N" then do
  268.                             nonstylecode = "nspace"
  269.                             end
  270.                         when left(code,1) = "T" then do
  271.                             nonstylecode = "thinspace"
  272.                             end
  273.                         when left(code,1) = "s" then do
  274.                             nonstylecode = "tab"
  275.                             end
  276.                         when left(code,1) = "-" then do
  277.                             nonstylecode = "softhyphen"
  278.                             end
  279.                         otherwise do
  280.                             nonstylecode = ""
  281.                             end
  282.                     end
  283.                 when code = "dp" then do
  284.                     paraname = ""
  285.                     paracode = code
  286.                     position = position+3
  287.                     end
  288.                 when code = "dP" then do
  289.                     position1 = pos(">",text,position)
  290.                     position = position+4
  291.                     oldname = paraname
  292.                     paraname = "<"||substr(text, position, position1-position)||">"
  293.                     if paraname~=oldname  then change = 1
  294.                     oldname = paracode
  295.                     paracode = "dP"
  296.                     if paracode~=oldname  then change = 1
  297.                     position = position1+1
  298.                     end
  299.                 when code = "ds" then do
  300.                     stylename = ""
  301.                     stylecode = code
  302.                     position = position+3
  303.                     end
  304.                 when code = "dS" then do
  305.                     position1 = pos(">",text,position)
  306.                     position = position+4
  307.                     stylename = substr(text,position,position1-position)
  308.                     tagdata = ppm_GetStyleTagData(stylename)
  309.                     call parsetag(tagdata) /* new style so reset all variables  */
  310.                     change = 0
  311.                     stylecode = "dS"
  312.                     position = position1+1
  313.                     end
  314.                 when verify(left(code,1),"bB","m")~=0 then do
  315.                     oldname = bold
  316.                     bold = "\"||left(code,1)
  317.                     position = position+2
  318.                     if bold~=oldname  then change = 1
  319.                     end
  320.                 when verify(left(code,1),"iI","m")~=0 then do
  321.                     oldname = italic
  322.                     italic = "\"||left(code,1)
  323.                     position = position+2
  324.                     if italic~=oldname  then change = 1
  325.                     end
  326.                 when verify(left(code,1),"uU","m")~=0 then do
  327.                     oldname = underline
  328.                     underline = "\"||left(code,1)
  329.                     position = position+2
  330.                     if underline~=oldname  then change = 1
  331.                     end
  332.                 when verify(left(code,1),"oO","m")~=0 then do
  333.                     oldname = outline
  334.                     outline = "\"||left(code,1)
  335.                     position = position+2
  336.                     if outline~=oldname  then change = 1
  337.                     end
  338.                 when left(code,1) = "n" then do
  339.                     typestyle = ""
  340.                     if bold~="" then change = 1
  341.                     bold = "\b"
  342.                     if italic~="" then change = 1
  343.                     italic = "\i"
  344.                     if underline~="" then change = 1
  345.                     underline = "\u"
  346.                     if outline~="" then change = 1
  347.                     outline = "\o"
  348.                     position = position+2
  349.                     end
  350.                 when code = "ff" then do
  351.                     position1 = pos(">",text,position)
  352.                     position = position+4
  353.                     oldname = fontname
  354.                     fontname = substr(text,position,position1-position)
  355.                     if fontname~=oldname  then change = 1
  356.                     position = position1+1
  357.                     end
  358.                 when code = "fs" then do
  359.                     position1 = pos(">",text,position)
  360.                     position = position+4
  361.                     oldname = fontsize
  362.                     fontsize = substr(text,position,position1-position)
  363.                     if fontsize~=oldname  then change = 1
  364.                     position = position1+1
  365.                     end
  366.                 when verify(left(code,1),"kK","m")~=0 then do
  367.                     oldname = kerning
  368.                     kerning = "\"||left(code,1)
  369.                     position = position+2
  370.                     if kerning~=oldname  then change = 1
  371.                     end
  372.                 when verify(left(code,1),"hH","m")~=0 then do
  373.                     oldname = hyphenation
  374.                     hyphenation = "\"||left(code,1)
  375.                     if hyphenation~=oldname  then change = 1
  376.                     position = position+2
  377.                     end
  378.                 when code = "lr"|code = "lf"|code = "ll" then do
  379.                     position1 = pos(">",text,position)
  380.                     position = position+4
  381.                     oldname = linespace
  382.                     linespace = substr(text,position,position1-position)
  383.                     if linespace~=oldname  then change = 1
  384.                     oldname = linespacecode
  385.                     linespacecode = code
  386.                     if linespacecode~=oldname  then change = 1
  387.                     position = position1+1
  388.                     end
  389.                 when code = "ls" then do
  390.                     position1 = pos(">",text,position)
  391.                     position = position+4
  392.                     oldname = lineshift
  393.                     lineshift = substr(text,position,position1-position)
  394.                     if lineshift~=oldname  then change = 1
  395.                     position = position1+1
  396.                     end
  397.                 when left(code,1) = "t" then do
  398.                     position1 = pos(">",text,position)
  399.                     position = position+3
  400.                     oldname = tracking
  401.                     tracking = substr(text,position,position1-position)
  402.                     if tracking~=oldname  then change = 1
  403.                     position = position1+1
  404.                     end
  405.                 when left(code,1) = "c" then do
  406.                     position1 = pos(">",text,position)
  407.                     position = position+3
  408.                     oldname = colour
  409.                     colour = substr(text,position,position1-position)
  410.                     if colour~=oldname  then change = 1
  411.                     position = position1+1
  412.                     end
  413.                 when code = "jl"|code = "jr"|code = "jc"|code = "jf" then do
  414.                     oldname = justification
  415.                     justification = "\"||code
  416.                     if justification~=oldname  then change = 1
  417.                     position = position+3
  418.                     end
  419.                 when code = "DC" then do
  420.                     position = pos("}>",text,position)+2
  421.                     nonstylecode = "definecolor"
  422.                     change = 0
  423.                     notcode = 1
  424.                     end
  425.                 otherwise position = position+2  
  426.                 end      /* of select */
  427.         end   /* of do forever */
  428. position = position+1
  429. return position
  430.  
  431.  
  432. /*  +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++  */
  433.  
  434. /* Parse a style tag definition  */
  435. parsetag:
  436.  
  437. parse arg tagdata
  438. tlength = length(tagdata)
  439. tposition = pos("\",tagdata)
  440. if pos = 0 then return
  441.  
  442. /* Clear all codes - the only compulsory info is the tag's name */
  443.         paraname = ""
  444.         paracode = ""
  445.         fontname = ""
  446.         typestyle = ""
  447.         fontsize = ""
  448.         bold = ""
  449.         italic = ""
  450.         underline = ""
  451.         outline = ""
  452.         kerning = ""
  453.         hyphenation = ""
  454.         linespace = ""
  455.         linespacecode = ""
  456.         lineshift = ""
  457.         tracking = ""
  458.         colour = ""
  459.         justification = ""
  460.  
  461. do forever
  462.     if substr(tagdata,tposition,1)~="\" then break
  463.     code = substr(tagdata,tposition+1,2)
  464.     select
  465.         when code = "dp" then do
  466.             paraname = ""
  467.             paracode = code
  468.             tposition = tposition+3
  469.             end
  470.         when code = "dP" then do
  471.             tposition1 = pos(">",tagdata,tposition)
  472.             tposition = tposition+4
  473.             paraname = "<"||substr(tagdata,tposition,tposition1-tposition)||">"
  474.             paracode = code
  475.             tposition = tposition1+1
  476.             end
  477.         when verify(left(code,1),"bB","m")~=0 then do
  478.             bold = "\"||left(code,1)
  479.             tposition = tposition+2
  480.             end
  481.         when verify(left(code,1),"iI","m")~=0 then do
  482.             italic = "\"||left(code,1)
  483.             tposition = tposition+2
  484.             end
  485.         when verify(left(code,1),"uU","m")~=0 then do
  486.             underline = "\"||left(code,1)
  487.             tposition = tposition+2
  488.             end
  489.         when verify(left(code,1),"oO","m")~=0 then do
  490.             outline = "\"||left(code,1)
  491.             tposition = tposition+2
  492.             end
  493.         when left(code,1) = "n" then do
  494.             typestyle = "\n"
  495.             bold = ""
  496.             italic = ""
  497.             underline = ""
  498.             outline = ""
  499.             tposition = tposition+2
  500.             end
  501.         when code = "ff" then do
  502.             tposition1 = pos(">",tagdata,tposition)
  503.             tposition = tposition+4
  504.             fontname = substr(tagdata,tposition,tposition1-tposition)
  505.             tposition = tposition1+1
  506.             end
  507.         when code = "fs" then do
  508.             tposition1 = pos(">",tagdata,tposition)
  509.             tposition = tposition+4
  510.             fontsize = substr(tagdata,tposition,tposition1-tposition)
  511.             tposition = tposition1+1
  512.             end
  513.         when verify(left(code,1),"kK","m")~=0 then do
  514.             kerning = "\"||left(code,1)
  515.             tposition = tposition+2
  516.             end
  517.         when verify(left(code,1),"hH","m")~=0 then do
  518.             hyphenation = "\"||left(code,1)
  519.             tposition = tposition+2
  520.             end
  521.         when code = "lr"|code = "lf"|code = "ll" then do
  522.             tposition1 = pos(">",tagdata,tposition)
  523.             tposition = tposition+4
  524.             linespace = substr(tagdata,tposition,tposition1-tposition)
  525.             linespacecode = code
  526.             tposition = tposition1+1
  527.             end
  528.         when code = "ls" then do
  529.             tposition1 = pos(">",tagdata,tposition)
  530.             tposition = tposition+4
  531.             lineshift = substr(tagdata,tposition,tposition1-tposition)
  532.             tposition = tposition1+1
  533.             end
  534.         when left(code,1) = "t" then do
  535.             tposition1 = pos(">",tagdata,tposition)
  536.             tposition = tposition+3
  537.             tracking = substr(tagdata,tposition,tposition1-tposition)
  538.             tposition = tposition1+1
  539.             end
  540.         when left(code,1) = "c" then do
  541.             tposition1 = pos(">",tagdata,tposition)
  542.             tposition = tposition+3
  543.             colour = substr(tagdata,tposition,tposition1-tposition)
  544.             tposition = tposition1+1
  545.             end
  546.         when code = "jl"|code = "jr"|code = "jc"|code = "jf" then do
  547.             justification = "\"||code
  548.             tposition = tposition+3
  549.             end
  550.         otherwise tposition = tposition+2
  551.         end
  552. end
  553. return
  554.  
  555. /*  ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++  */
  556.  
  557. exit_msg:
  558. do
  559.     parse arg message
  560.     if message ~="" then call ppm_inform(1,message,"Resume")
  561.     call ppm_ClearStatus()
  562.     call ppm_SetColorMode(colormode)
  563.     call ppm_AutoUpdate(1)
  564.     exit
  565. end
  566.  
  567.