0

I have a directory with many HTML documents. Most of them contain the codeblock

      .org-link {
        /* org-link */
        color: #b58900;
        font-weight: bold;
        text-decoration: underline;
      }

inside the <style type="text/css"> tag. I'd like to write a script that removes the line text-decoration: underline; and changes the color to #2aa198 from this block in every file.

Is it possible to accomplish this with python?

3
  • is there only one text-decoration css attribute in the file? Commented Jan 12, 2016 at 8:28
  • no there are several in other blocks. Commented Jan 12, 2016 at 8:29
  • possible duplicate stackoverflow.com/questions/30360290/… Commented Jan 12, 2016 at 8:42

1 Answer 1

2

You could use regular expressions to make the necessary replacements as follows:

import re

test = """
      .org-link {
        /* org-link */
        color: #b58900;
        font-weight: bold;
        text-decoration: underline;
      }
"""

def fix(org_link):
    new_color = re.sub(r'(.*?color\s*?:\s*?)(.*?)(;)', r'\1#777\3', org_link.group(0), flags=re.S)
    return re.sub(r'(.*?)(\s+?text-decoration: underline;)(.*?)', r'\1\3', new_color, flags=re.S)

print re.sub(r'(org-link\s+\{.*\})', fix, test, flags=re.S)

This would convert the text as follows:

  .org-link {
    /* org-link */
    color:#777;
    font-weight: bold;
  }

It works by first identifying suitable org-link blocks and then first replacing the color and then removing any text-decoration entries.

The script could then be extended to carry this out on all of the HTML files in a given folder as follows:

import re
import glob

def fix(org_link):
    new_color = re.sub(r'(.*?color\s*?:\s*?)(.*?)(;)', r'\1#777\3', org_link.group(0), flags=re.S)
    return re.sub(r'(.*?)(\s+?text-decoration: underline;)(.*?)', r'\1\3', new_color, flags=re.S)

for html_file in glob.glob('*.html'):
    print html_file
    with open(html_file) as f_input:
        html = re.sub(r'(org-link\s+\{.*\})', fix, f_input.read(), flags=re.S)

    with open(html_file, 'w') as f_output:
        f_output.write(html)

Tested using Python 2.7.9

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.