예제 #1
0
파일: AnkiIR.py 프로젝트: YDEKQ/anki-ir
def doAddURL():
    # get URL from clipboard
    url = str(QApplication.clipboard().mimeData().text())
    try:
        # download HTML file with urllib
        html = opener.open(url).read()
    except ValueError:
        utils.showInfo("Please copy a URL to clipboard first.")
        return
    # parse HTML and find images
    xml = libxml2.htmlParseDoc(html, 'utf-8')
    context = xml.xpathNewContext()
    # find correct nodes via XPath
    count = 0
    for img in context.xpathEval('//img'):
        # get src attribute
        attr = img.get_properties()
        imgurl = None
        while attr:
            if attr.name == 'src':
                _replaceImageSrc(url, attr)
                count += 1
                break
            attr = attr.get_next()

    # add new fact
    fact = Fact(mw.deck.currentModel)
    val = tidyHTML(xml.serialize(encoding='utf-8').decode('utf-8', 'replace'))
    fact.fields[0].value = val
    mw.deck.addFact(fact, reset=True)
    utils.showInfo(
        "URL successfully added as new fact (%d pictures downloaded)" % count)
예제 #2
0
 def createFact(self, attrs):
     """Makes a new Anki fact from an entry."""
     model = self.models[attrs["et"]]
     self.f = Fact(model)
     # process attributes
     for attr in ["a1", "a2" "a3", "a4"]:
         if attr in attrs.keys():
             self.f.__setitem__(
                 self.typeAttributes[attrs["et"] + "_" + attr],
                 self.attributeItems[attrs[attr]])
     # process tags. Unit, Category plus entry type name
     tagString = unicode(self.unitCategories[attrs["u"]] + " " +
                         self.unitCategories[attrs["c"]] + " " + model.tags)
     self.f.tags = tagString