Mercurial > js4py
view py/gotcha.py @ 162:77e87f1b5811 core-javascript
$ hg merge core-javascript.closure-exercises # done, at least for now
| author | jf3243 |
|---|---|
| date | Wed, 09 Nov 2011 17:00:38 +0000 |
| parents | c9fe170162ee |
| children |
line wrap: on
line source
import string def split(s): for item in s.split('&&&'): yield map(string.strip, item.split('===\n')) QUESTION_TEMPLATE = ''' .. _%(label)s-gotcha: **%(name)s** .. code-block:: javascript %(js)s :ref:`Answer <%(label)s-gotcha-answer>`. ''' ANSWER_TEMPLATE = ''' .. _%(label)s-gotcha-answer: **%(name)s** .. code-block:: javascript %(js)s %(answer)s :ref:`Question <%(label)s-gotcha>`. ''' class Gotcha(object): def __init__(self, name, js, answer): self.name = name self.js = '\n'.join([' ' + line for line in js.split('\n')]) self.answer = answer self.answer = '\n'.join([' ' + line for line in answer.split('\n')]) self.label = name.lower().replace(' ', '-') def as_question(self): return QUESTION_TEMPLATE % (self.__dict__) def as_answer(self): return ANSWER_TEMPLATE % (self.__dict__) def __str__(self): return '<Gotcha %r>' % self.name __repr__ = __str__ if __name__ == '__main__': import os s = open('txt/gotcha.txt').read() # Prefix a sort if you wish. gotchas = [Gotcha(*x) for x in split(s)] for key in ('question', 'answer'): f = open(os.path.join('generated', 'gotcha-%ss.txt' % key), 'wb') for gotcha in gotchas: f.write(getattr(gotcha, 'as_' + key)()) f.close()
