1

Hi guys having trouble with a particular problem. I am using python's regex to alter the markup source to output html format.

markup source:

[ 
# sometextsometextsometextsometextsometextsometext.  #

# sometextsometextsometextsometextsometextsometextsometextsometext
sometextsometextsometextsometextsometextsometext. #
]


[
hello i am a normal paragraph.
]

desired output:

<ol> 
<li> sometextsometextsometextsometextsometextsometext.  </li>

<li> sometextsometextsometextsometextsometextsometextsometextsometext
sometextsometextsometextsometextsometextsometext. </li>
</ol>

<p>
hello i am a normal paragraph.
</p>
5
  • 1
    How is the code supposed to know whether to put the text in a list or a paragraph? Commented May 8, 2013 at 0:51
  • The presence of a '#' within the square brackets.. i guess, not entirely sure. Commented May 8, 2013 at 0:57
  • 3
    So what exactly is your problem, and what solutions have you tried? Commented May 8, 2013 at 1:00
  • I need to distinguish between a [text] and a [#list#] sub(r'\#(.*?)\#', '<li>\g<1></li>', data) partially works (works for the first line of sometext, not the second. Commented May 8, 2013 at 1:23
  • just an idea: regex101.com/r/iG0qF0 Commented May 8, 2013 at 1:39

1 Answer 1

1
import re
with open('mk.txt') as f:
    with open('newmk.txt','w+') as g:
        text = f.read()
        SquareGroups = re.findall(r'\[(?:.|\n)+?\]',text)
        for group in SquareGroups:
            if '#' in group: #must be ol
                group = group.replace('[','<ol>')
                group = group.replace(']','</ol>')
                group = re.sub('#(?= ?\w)','<li>',group)
                group = re.sub('(?<=[\w ])#','</li>',group)
            else:
                group = group.replace('[','<p>')
                group = group.replace(']','</p>')
            g.write(group)
            g.write('\n') #optional, just makes the output look 'nicer'

Transforms your input in mk.txt into the following text in newmk.txt:

<ol>
<li> sometextsometextsometextsometextsometextsometext.  </li>

<li> sometextsometextsometextsometextsometextsometextsometextsometext
sometextsometextsometextsometextsometextsometext. </li>
</ol>
<p>
hello i am a normal paragraph.
</p>
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.