nouptime
nouptime

Reputation: 10447

Convert inline styles to <font> tags including attributes

I'm working with a legacy system that does not support inline style or CSS input. There is a large number of HTML files that need to be converted to a specific format to be compatible with this system.

So I'm looking for a way to progrmmatically convert the inline styles of several tags to individual font tags with the relevant attributes.

Can this be done? For example:

<p style="color: #000; font-family: arial,helvetica,sans-serif; font-size: 10px; font-weight: bold;">Text</p>
<!-- Converted to: -->
<font color="#000" face="arial,helvetica,sans-serif" size="10px" weight="bold"><p>Text</p></font>

EDIT: Not a duplicate of What tag should I use instead of deprecated tag font in html (cannot use CSS). I am not looking for an alternative to the font tag since style attributes get stripped in the legacy system.

Upvotes: 0

Views: 283

Answers (1)

cssyphus
cssyphus

Reputation: 40106

Yes, it can be done. You need a programmable scripting language that can read/write text files, perform basic logic, loops, etc. There are several to choose from, varying in level of difficulty. Examples include:

  1. VBA
  2. Windows PowerShell
  3. PHP (install XAMPP and you will have access to this powerful language)
  4. node.js
  5. python
  6. WinBatch
  7. AutoHotKey (AHK) or AutoIT (AHK forked off from AutoIT)

For ease of use, my preferences would be ranked in this order: 6, 3, 5, 1, 4, 2, 7.

Pick one, then begin trying to do the project, then come back and ask us for more help if you are stuck. Basically, a pseudo-code algorithm might look something like this:

arr = array_of_the_html_filenames
for i = 1 to len(arr) //i.e. do this for each filename
    next_file_name = arr[i]
    func_process_this_file(next_file_name)
next

func_process_this_file(file_name)
    input_file_name = file_name
    output_file_name = parse input_file_name string to create an output_file_name
    hFIN = fileOpen(input_file_name, "read") #get fileHandle for next file
    hFOUT = fileOpen(output_file_name, "write")

    next_line = fileRead(hFIN) //read next_line of current file as a string
    while next_line !== "EOF"
        out_line = ''
        if next_line == EOF: break
        if next_line contains "font-family":
            font_data = parse the string to get the data for the font tag
            rest_of_string_with_font_data_removed = parse string to extract all except font data
            out_line = "<font>" + font_data + "</font>" + rest_of_string_with_font_data_removed
            file_write(hFOUT, out_line)
        else
            out_line = next_line
            file_write(hFOUT, out_line)
        endif
        next_line = fileRead(hFIN) //read next_line of current file as a string
    endwhile
    file_close(hFIN)
    file_close(hFOUT)
return

Upvotes: 1

Related Questions