/* CSStoFMText ( text )
by Christopher Gauntt, Cordega Solutions, www.cordega.com
solutions@cordega.com
v2.5 FEB 15, 2020 steve_ssh
- fix Subscript and Superscript
v2.4 APR 2, 2019 Josh Willing
- add HighlightYellow style detection
v2.3 APR 1, 2019 Josh Willing
- don't confuse background-color and color. Doesn't copy background color yet, but font color is fixed.
v2.2 Mar 26, 2019 Josh Willing
- added support for
tag (including slash)
- reformat function text
- Don't apply transformations if none exists
- Use internal let variables
- remove fontscript reference. FM no longer uses it as of 15: https://fmhelp.filemaker.com/help/15/fmp/en/index.html#page/FMP_Help/textfont.html
v2.1 May 15, 2010
- Fixed the errors brought up in the comments. Sorry it took so long to get around to it! Thanks to Kevin Frank for spotting the comment, and sending me a fixed version.
v2.0 April 06, 2009
- now includes if statements wrapped around main function in case input text doesn't have any SPAN tags. (Otherwise, part of the beginning of the text gets chopped off)
- simplified conversion of Hex Color to straight color decimal value instead of intermediary RGB.
Description:
This takes CSS styled text and converts it back to FileMaker Pro formatting.
NOTES:
- GetAsCSS does not detect the HighlightYellow style, so we don't include.
*/
If (PatternCount (Text ; "" ; "¶" ) ;
Let ([
// Grabs SPAN string
~SpanStart = Position ( text ; "" ; ~SpanStart ; 1 ) +1 ;
~Span = Middle ( text ; ~SpanStart ; ~SpanEnd - ~SpanStart ) ;
// Grabs Full SPAN string between SPAN tags
~FullSpanStart = Position ( text ; "" ; ~FullSpanStart ; 1 ) + 7 ;
~FullSpan = Middle ( text ; ~FullSpanStart ; ~FullSpanEnd - ~FullSpanStart ) ;
// Grabs only text between span tags
~TextStart = Position ( ~FullSpan ; ">" ; 0; 1 )+1 ;
~TextEnd = Position ( ~FullSpan ; "" ; 0 ; 1 ) ;
~TextSpan = Middle ( ~FullSpan ; ~TextStart ; ~TextEnd - ~TextStart ) ;
// swaps out breaks with FileMaker return markers
~Text = Substitute (
~TextSpan ;
["
" ; "¶" ];
["
" ; "¶" ]
) ;
// Collects Text Styles
~styles = If ( PatternCount ( ~Span ; "Bold" ) = 1 ; "+Bold" ; "" )
& If ( PatternCount ( ~Span ; "Italic" ) = 1 ; "+Italic" ; "" )
& If ( PatternCount ( ~Span ; "Underline" ) = 1 ; "+Underline" ; "" )
& If ( PatternCount ( ~Span ; "Condense" ) = 1 ; "+Condense" ; "" )
& If ( PatternCount ( ~Span ; "Extend" ) = 1 ; "+Extend" ; "" )
& If ( PatternCount ( ~Span ; "Strikethrough" ) = 1 ; "+Strikethrough" ; "" )
& If ( PatternCount ( ~Span ; "SmallCaps" ) = 1 ; "+SmallCaps" ; "" )
& If ( PatternCount ( ~Span ; "vertical-align: text-top" ) = 1 ; "+Superscript" ; "" )
& If ( PatternCount ( ~Span ; "vertical-align: text-bottom" ) = 1 ; "+Subscript" ; "" )
& If ( PatternCount ( ~Span ; "Uppercase" ) = 1 ; "+Uppercase" ; "" )
& If ( PatternCount ( ~Span ; "Lowercase" ) = 1 ; "+Lowercase" ; "" )
& If ( PatternCount ( ~Span ; "Titlecase" ) = 1 ; "+Titlecase" ; "" )
& If ( PatternCount ( ~Span ; "WordUnderline" ) = 1 ; "+WordUnderline" ; "" )
& If ( PatternCount ( ~Span ; "DoubleUnderline" ) = 1 ; "+DoubleUnderline" ; "" )
& If ( PatternCount ( ~Span ; "background-color: #FFFF00" ) = 1 ; "+HighlightYellow" ; "" ) ;
~stylesTrue = If ( IsEmpty (~styles) ; "" ; Right (~styles ; Length (~styles) -1) ) ;
// Parses out font size
~FontSize = If (
PatternCount (~Span ; "font-size") = 0 ;
"" ;
Let ( [
~SizeStart = Position ( ~Span; "font-size"; 0; 1) ;
~SizeStop = Position ( ~Span; "px"; ~SizeStart; 1) ;
~SizeString = Middle ( ~Span; ~SizeStart ; ~SizeStop - ~SizeStart + 1 )
];
Filter ( ~SizeString; "0123456789")
)
) ;
// Adjusts font size for Sub/Super-script
~FontSize = Case (
PatternCount ( ~styles ; "Superscript" ) + PatternCount ( ~styles ; "Subscript" ) = 0 ;
~FontSize ;
~FontSize * 2
) ;
// Parses out font-family
~FontType = If (
PatternCount (~Span ; "font-family") = 0 ;
"" ;
Let ( [
~FontStart = Position ( ~Span; "font-family"; 0; 1) ;
~FontStartTrue = Position ( ~Span; "'"; ~FontStart; 1) ;
~FontStop = Position ( ~Span; "'"; ~FontStart; 2)
];
Substitute ( Middle ( ~Span; ~FontStartTrue ; ~FontStop - ~FontStartTrue + 1 ) ; "'" ; "\"" )
)
) ;
//Converts Font Color
~FontColor = If (
PatternCount (~Span ; ";color:") = 0 and PatternCount (~Span ; "\"color:") = 0 ;
"" ;
Let ( [
~colorStart = Position ( ~Span; "#"; 0; 1) ;
~colorHex = Middle ( ~Span; ~colorStart + 1 ; 6 ) ;
~HexClean = Trim ( Filter ( Upper ( ~colorHex ) ; "0123456789ABCDEF" ) ) ;
~HexSplit =
"(" & ( Middle ( ~HexClean ; 1 ; 1 ) & "* 16 + " & Middle ( ~HexClean ; 2 ; 1 ) ) & ")* 65536 +" &
"(" & ( Middle ( ~HexClean ; 3 ; 1 ) & "* 16 + " & Middle ( ~HexClean ; 4 ; 1 ) ) & ")* 256 +" &
"(" & ( Middle ( ~HexClean ; 5 ; 1 ) & "* 16 + " & Middle ( ~HexClean ; 6 ; 1 ) ) & ")"
];
Evaluate ( Substitute ( ~HexSplit ; [ "A" ; 10 ] ; [ "B" ; 11 ] ; [ "C" ; 12 ] ; [ "D" ; 13 ] ; [ "E" ; 14 ] ; [ "F" ; 15 ] ) )
)
) ;
// converts the text string between the span tags into the filemaker conversions from the above
~ConvertedText = If ( IsEmpty ( ~stylesTrue ) ; ~Text ; TextStyleAdd ( ~Text ; Evaluate ( ~stylesTrue ) ) ) ;
~ConvertedText = If ( IsEmpty ( ~FontSize ) ; ~ConvertedText ; TextSize ( ~ConvertedText ; ~FontSize ) ) ;
~ConvertedText = If ( IsEmpty ( ~FontType ) ; ~ConvertedText ; TextFont ( ~ConvertedText ; Evaluate (~FontType) ) ) ;
~ConvertedText = If ( IsEmpty ( ~FontColor ) ; ~ConvertedText ; TextColor ( ~ConvertedText ; ~FontColor ) ) ;
// replaces the span string with the converted string.
~NewText = Substitute ( text ; ~FullSpan ; ~ConvertedText )
];
// returns converted text, or looks for more spans to swap out.
If (
PatternCount ( ~NewText ; " 0 ;
CSStoFMText ( ~NewText ) ;
~NewText
)
) // End Let
) // End If