Advertisement
AZJIO

doc to html (Clear HTML)

Oct 5th, 2022
2,262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
AutoIt 2.06 KB | None | 0 0
  1. #include <Array.au3>
  2. ; $sText = FileRead(@ScriptDir & '\1.htm')
  3. $sText = FileRead(@ScriptDir & '\site.htm')
  4.  
  5. $sText = _ClearHTML($sText)
  6.  
  7. $hFile = FileOpen(@ScriptDir & '\2.htm', 2)
  8. FileWrite($hFile, $sText)
  9. FileClose($hFile)
  10.  
  11.  
  12. Func _ClearHTML($sText)
  13.     Local $0, $DelAll, $DelTag, $i, $Rep, $teg, $Tr
  14.     $Tr = 0
  15.     $teg = 'p|div|span|html|body|b|table|td|tr|th|font|img|br'
  16.     $sText=StringRegExpReplace($sText, '(?s)<!--.*?-->', '') ; удаление комментариев
  17.    
  18.     ; ============= блок colspan, rowspan
  19.     $0 = Chr(0)
  20.     $Rep = StringRegExp($sText, '(?s)(<[^<>]+?(?:colspan|rowspan)[^<>]+?>)', 3) ; в массив
  21.     If Not @error Then
  22.         $Tr = 1
  23.         $sText = StringRegExpReplace($sText, '(?s)(<[^<>]+?(?:colspan|rowspan)[^<>]+?[^/]>)', $0) ; временная подмена
  24.     EndIf
  25.     $sText = StringRegExpReplace($sText, '(?s)(<(?:' & $teg & '))[\r\n]* [^<>]*?(>)', '\1\2') ; очистка
  26.    
  27.     If $Tr Then
  28.         For $i = 0 To UBound($Rep) - 1
  29.             $Rep[$i] = _Replace($Rep[$i])
  30.             $sText = StringReplace($sText, $0, $Rep[$i], 1)
  31.         Next
  32.     EndIf
  33.     ; =============
  34.    
  35.     $sText=StringReplace($sText, '<p><o:p>&nbsp;</o:p></p>', '<br><br>') ; замена переносов строк
  36.     ; $sText=StringRegExpReplace($sText, '(?s)(<('&$teg&').*?>)(.*?)</\2>(\s*)\1', '\1\3\4') ; очистка дублирования
  37.     $sText=StringRegExpReplace($sText, '(?s)<('&$teg&')[^<>]*?>[\x{A0}\s]*?</\1>', '') ; очистка тегов без контента
  38.    
  39.     $DelAll='xml|style|script'
  40.     $sText=StringRegExpReplace($sText,'(?s)<('&$DelAll&')[^<>]*>(.*?)</\1>','') ; удаление с содержимым
  41.    
  42.     $DelTag='span'
  43.     $sText=StringRegExpReplace($sText,'(?s)</?('&$DelTag&')[^<>]*>','') ; удаление самих тегов
  44.    
  45.     Return $sText
  46. EndFunc
  47.  
  48. Func _Replace($Rep)
  49.     $teg = 'table|td|tr|th'
  50.     $aRep=StringRegExp($Rep, '((?:colspan|rowspan)\h*=\h*"?\d+"?)', 3)
  51.     $Rep = StringRegExpReplace($Rep, '(?s)(<(?:' & $teg & ')) .*?(>)', '\1') ; очистка
  52.     For $i = 0 To UBound($aRep)-1
  53.         $Rep &= ' ' & $aRep[$i]
  54.     Next
  55.     $Rep &= '>'
  56.     Return $Rep
  57. EndFunc
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement