Beispiel #1
0
    def updatePost(self, post, txt) :
        # Check if there is no id.

        mdict = formatter.metadataDict(txt)
        content = formatter.getContent(txt)
        post = self.appendMetadataToPost(mdict, post)

        printDebug("STEP", "Updating post .. ", post.title)
        assert post.post_type
        # content 
        if content :
            if len(content.strip()) == 0 :
                print("[E] : No content in file.")
                return 
        else :
            printDebug("WARN", "Post with empty content.")
            content = ""
    
        if self.format == "html":
            content = formatter.htmlToHtml(content)
        elif self.format == "markdown":
            content = formatter.markdownToHtml(content)
            # Fix the math class 
            pat = re.compile(r'\<span\s+class\=\"math\"\>\\\((.+?)\\\)\<\/span\>'
                    , re.DOTALL)
            for m in pat.findall(content):
                printDebug("INFO", "Latex expression: ", m) 
            content = pat.sub(r'$latex \1$', content)
                
            post.content = content.encode('utf-8')
        else:
            post.content = content.encode('utf-8')

        #logFile = os.path.join(self.blogDir, "sent_html") 
        #printDebug("DEBUG", "Logging content into %s" % logFile)
        #with open(logFile, "w") as f:
            #f.write(post.content.encode('utf-8'))

        printDebug("STEP"
                , "Sending post : {0} : {1}.".format(post.id, post.title)
                )
        try:
            self.wp.call(EditPost(post.id, post))
        except Exception as e:
            printDebug("DEBUG", "I was trying to update but failed")
            printDebug("INFO", "Error was : {0}".format(e))
            printDebug("DEBUG", "Content of post was written to log.txt file")
            with open("log.txt", "w") as f:
                f.write(post.content)
            sys.exit(0)
Beispiel #2
0
    def writeContent(self, fH, content, format):
        """Write content to file.
        """
        newContent = ["<p>"]
        for line in content.split('\n'):
            if len(line.strip()) == 0:
                newContent.append("</p>\n<p>")
            else:
                newContent.append(line)
        newContent.append('<\p>')
        content = '\n'.join(newContent)

        if format == "html":
            logging.info("Writing html content")
            content = formatter.htmlToHtml(content) 
        elif format in ["markdown", "md"]:
            logging.info("Writing markdown format")
            content = formatter.htmlToMarkdown(content)
        fH.write(content.encode('utf-8'))