Question

I have html-template in my project that they are contain pystache codes such as

{{#_}}Word{{\_}} 

I want to know , how I can extract this words by PoEditor parsers

Was it helpful?

Solution

You could use a regular expression to get them, and then remove what you don't want:

import re
regex=re.compile("\{\{\#\_\}\}.+\{\\\_\}\} ")
words=re.findall(regex, data) 
#To remove it use re.split or simply now searching for [A-Z].

OTHER TIPS

now I just using this code for making a file for using in PoEdit for scan and extracting the keywords:

def makeTempLang():
    fs = getFiles('templates/')
    words = []

    regex =re.compile("\{\{\#\_\}\}(.+)\{\{/\_\}\}")

    for f in fs:
        data=open(f,'r').read()
        fwords=re.findall(regex, data)
        words.extend(fwords)

    clean = (words[4:])
    data='from core import config\n_=config.i18n\n'
    for c in clean:
        data = "%s_('%s')\n"%(data,c)
    open('locale/temp2.py','w+').write(data)

    pass

def getFiles(spath=''):
    res =[]

    arr = os.listdir(spath)

    for d in arr:
        dpath =os.path.join(spath,d)

        if d.endswith('.htm'):
            res.append(dpath)
        if os.path.isdir(dpath):
            sub=getFiles(dpath)
            if len(sub) > 0 :
                res.extend(sub)
    return res
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top