Find valid HTML Tag in String
lazarus / 21-General
----------------------------------------------------------------------
Maris · 2023-08-23 13:14
Hi!
I wanted to ask for help with probably very simple problem. What I
need to do, is to check a string for any valid HTML tag. Examples:
Code: Pascal [Select][+][-]window.onload = function(){var x1 =
document.getElementById("main_content_section"); if (x1) { var x =
document.getElementsByClassName("geshi");for (var i = 0; i "Some text
more text
" {True}"Some text < p >more text" {True}"Some
text more text
" {True}"In range < 1 meter
and > 1 cm" {False}"Must be >1m" {False}
Is there any simple way to do this? Preferably with build-in units to
maintain code easier. At least any built-in unit with full list of
valid HTML tags would be nice, which I couldn't find.
In more details (maybe there is better way in general). What I'm
trying to do is to convert Excel with HTML code to .html file to
proces it in CAT Tools. Afterwords when everything is done it will be
imported back to excel in appropriate cells. Problems showed up when I
noticed some columns has plain text only and even more some HTML
columns contains plain text in some rows and some of them has symbols
> and < which are not encoded, so in general it's a plain text. If
symbols > and < are note encoded for non html cells, generated .html
file is not valid.
Algorithm could be like this:
Code: HTML5 [Select][+][-]window.onload = function(){var x1 =
document.getElementById("main_content_section"); if (x1) { var x =
document.getElementsByClassName("geshi");for (var i = 0; i if "<" or
">" found then if valid HTML tag found then begin add to output
as HTML "CellValue
" end else
begin encode html entities and add to output "EncodeHTMLEntities(CellValue)
" (where
"e" after cell address means encoded)-----------^ endelse add to
output "CellValue
" (Text without
"<" and ">" is ok to add in between div tags even if it's plain text)
Processed file will be imported back based on cell address and Decode
Entities will be used for cells marked with "e" (encoded).
Generated .html file would look like this:
Code: HTML5 [Select][+][-]window.onload = function(){var x1 =
document.getElementById("main_content_section"); if (x1) { var x =
document.getElementsByClassName("geshi");for (var i = 0; i
Some Text or HTML
code
Some Text or HTML
code
Some Text or HTML
code
Some Text or HTML
code
Some Text or HTML
code
Some Text or HTML
code
Some Text or HTML
code
Some Text or HTML
code
Some Text or HTML
code
Some Text or HTML
code
Thank you all in advance!
----------------------------------------------------------------------
Maris · 2023-08-23 14:35
After a while of searching, it looks like there is no ready made
solution for finding valid HTML tags in string, so best option would
be to search for "" pattern, extract "abc" part and compare
it with all possible tags. I found tags are defined in HTMLDefs unit,
so this would be useful probably.
Any other suggestions would be greatly appreciated.
----------------------------------------------------------------------
cdbc · 2023-08-24 05:43
Hi
If you've got this HTMLDef unit, couldn't you then put all these tags
into a e.g.: TStringHashTable from "contnrs" unit, as a snappy
lookup...
And then compare against the lookup-table, it's easy to write a "Find"
function for the hashtable...
Just my 2 cents worth
Regards Benny
----------------------------------------------------------------------
Zvoni · 2023-08-24 06:47
Untested:
Regex-Pattern:
(?:<\s*([a-zA-Z]+)\s*.*?>)(?:.*)(<\s*\/*(\1)\s*>)
https://regex101.com/r/x1TpVc/1
----------------------------------------------------------------------
Bart · 2023-08-24 08:23
We do have a fasthtmlparser unit IIRC.
Bart
----------------------------------------------------------------------
Zvoni · 2023-08-24 10:08
Quote from: Bart on August 24, 2023, 10:23:29 amWe do have a
fasthtmlparser unit IIRC.
Bart
https://github.com/z505/fast-html-parser
if it's tested, then yes: Use tried and trusted
----------------------------------------------------------------------
wp · 2023-08-24 10:19
fasthtmlparser is even included in fpc (folder packages/chm/src). It
is my "swiss army knife" when it comes to parsing html, and it is used
in TAChart and in fpspreadsheet.
----------------------------------------------------------------------
Maris · 2023-08-24 16:14
Thank you all for answers and suggestions!
As it was a bit urgent I made program in simple way. I'll take a look
on suggested units and data structures maybe it will be useful to make
code more readable or improve performance. Although I'm using OLE
objects, so performance cannot be very good anyway.
Main problem is that excel files with HTML code can contain anything
in-between and there is no guarantee code itself is valid. We
receiving files from different customers and we never know what is the
source. Could be export from some kind of CMS or anything else.
I used TStringList for list of HTML tags, also used old school Pos
instead of Regex and could solve task without fully parsing of HTML
code, at least for now.
The part which is checking if cell contains HTML code or plain text
and reading content from excel is as follows:
Code: Pascal [Select][+][-]window.onload = function(){var x1 =
document.getElementById("main_content_section"); if (x1) { var x =
document.getElementsByClassName("geshi");for (var i = 0; i uses
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ComCtrls,
LazFileUtils, FileUtil, ExtCtrls, Grids, ActnList, StdCtrls, EditBtn,
SynHighlighterHTML, SynEdit, ComObj, HTMLDefs; type {
TFormHTMLExcel } TFormHTMLExcel = class(TForm) ... private
HTMLTags: TStringList; ... procedure ExcelToHtml(FileName:
String; OutputDir: String); function TagFound(Code: String):
Boolean; Inline; public constructor Create(AOwner: TComponent);
override; destructor Destroy; override; end; var FormHTMLExcel:
TFormHTMLExcel; implementation {$R *.lfm} { TFormHTMLExcel
} constructor TFormHTMLExcel.Create(AOwner: TComponent);var i:
THTMLElementTag;begin inherited; HTMLTags := TStringList.Create;
HTMLTags.Sorted := True; for i in THTMLElementTag do begin
HTMLTags.Append(HTMLElementProps[i].Name); end;
HTMLTags.Append('!DOCTYPE'); {Add Doctype definition to tag
list}end; destructor TFormHTMLExcel.Destroy;begin inherited;
HTMLTags.Free;end; function TFormHTMLExcel.TagFound(Code: String):
Boolean; Inline;var TNStart, TNEnd, NextSpace: Word;begin TNStart :=
Pos('<', Code); if (TNStart > 0) and (TNStart < Length(Code)) then
begin TNEnd := Pos('>', Code, TNStart + 1); if TNEnd > 0 then
begin {Search for next non-space character after <} repeat
inc(TNStart); until (Code[TNStart] <> ' ') or (TNStart =
Length(Code)); {Condition TNStart = Length(Code) shouldn't happen
in theory, but who knows} {Search for next space or > symbol}
TNEnd := Pos('>', Code, TNStart + 1); NextSpace := Pos(' ',
Code, TNStart + 1); if (NextSpace > 0) and (NextSpace < TNEnd)
then begin TNEnd := NextSpace; end; if
HTMLTags.IndexOf(Copy(Code, TNStart, TNEnd - TNStart)) > -1 then
begin TagFound := True; end else begin
TagFound := False; end; end else begin {If no
closing greater then (>) symbol found, it must be plain text or HTML
code is not valid Do not process such code as HTML, consider it
as plain text to avoid mistakes in Trados} TagFound := False;
end; end else begin TagFound := False; end;end; procedure
TFormHTMLExcel.ExcelToHtml(FileName: String; OutputDir: String);const
AppName = 'Excel.Application';var App, Workbook: OLEVariant;
InputFile: Variant; ResultHTML: TStringList; r, c, s, RowCount,
ColumnCount: Word; CellValue: String;begin ResultHTML :=
TStringList.Create; App := CreateOleObject(AppName); try
App.Visible := False; App.DisplayAlerts := False; InputFile :=
FileName; Workbook := App.Workbooks.Open(InputFile); for s :=
1 to Workbook.Sheets.Count do begin
Workbook.Sheets.Item[s].Activate; RowCount :=
App.ActiveSheet.UsedRange.Rows.Count; ColumnCount :=
App.ActiveSheet.UsedRange.Columns.Count; ResultHTML.Clear;
ResultHTML.Append(SynEditHeader.Text); for c := 1 to ColumnCount
do begin for r := 1 to RowCount do begin
CellValue := App.Cells[r, c].Value; if Length(CellValue) > 0
then begin if (not TagFound(CellValue)) and
((Pos('<', CellValue) > 0) or (Pos('>', CellValue) > 0)) then
begin {Use Encoding only when < and > symbols are found
in text, looks like other HTML special characters does
not break structure and can be used as it is} CellValue
:= StringReplace(CellValue, '<', '<', [rfReplaceAll]);
CellValue := StringReplace(CellValue, '>', '>', [rfReplaceAll]);
ResultHTML.Append('' + CellValue + '
');
end else begin
ResultHTML.Append('' + CellValue + '
'); end;
end; end; end;
ResultHTML.Append(SynEditFooter.Text);
ResultHTML.SaveToFile(OutputDir + IntToStr(s) + '-' +
App.ActiveSheet.Name + '.html'); end; finally App.Quit; App
:= Unassigned; ResultHTML.Free; end;end;
In short, program populates String list with tag names in Constructor.
This list is used later to find valid tags in string. To read content,
program loops through all excel cells and search for any valid HTML
tags, if not found encodes < and > symbols only before storing result
in .html file. Not fully tested yet, but so far it can distinguish
cells with plain text like ">1m2", "<2m2" or similar and encode them
before saving to .html file. With more complicated content could be
less precise.
Another interesting thing is that Lazarus shows bunch of hints like
this:
Code: Pascal [Select][+][-]window.onload = function(){var x1 =
document.getElementById("main_content_section"); if (x1) { var x =
document.getElementsByClassName("geshi");for (var i = 0; i
unit1.pas(151,18) Note: Call to subroutine "operator :=(const
source:AnsiString):Variant;" marked as inline is not inlined
I assume it's implemented in newer versions of Lazarus as I do not
remember such hints before. Did a quick search about it and found it
is something about Variant variables, but as far as I know there is no
other way to work with office OLE objects.