Example #1
0
class TextEditDialog(QDialog):
    def __init__(self, font, textColor, parent=None):
        super(TextEditDialog, self).__init__(parent)
        self.setWindowFlags(QtCore.Qt.Window | QtCore.Qt.FramelessWindowHint)
        self.resize(QtCore.QSize(400, 300))
        self.layout = QVBoxLayout(self)
        self.layout.setContentsMargins(2, 2, 2, 2)
        self.te = QTextEdit()
        self._font = font
        self._font = QtGui.QFont(font)
        self.te.setFont(self._font)
        self.te.setTextColor(textColor)
        self.layout.addWidget(self.te)
        self.buttons = QDialogButtonBox(
            QDialogButtonBox.Ok | QDialogButtonBox.Cancel, self)
        self.buttons.accepted.connect(self.onAccept)
        self.buttons.rejected.connect(self.onReject)
        self.layout.addWidget(self.buttons)
        self._result = None
        self.te.textChanged.connect(
            lambda color=textColor: self.te.setTextColor(color))

    def zoomIn(self, factor):
        self.te.zoomIn(factor)

    def setHtml(self, html):
        self.te.setHtml(html)

    def onReject(self):
        self._result = "", False
        self.reject()

    def getResult(self):
        return self._result

    def onAccept(self):
        self.te.selectAll()
        # self._font.setPointSize(self.initialPointSize)
        self.te.setFont(self._font)
        self._result = self.te.toHtml(), True
        self.accept()
Example #2
0
def embed_readme_images(readme_path):
    """
    QTextEdit used to auto detect raw / html / markdown,
    and do the conversion to html.
    """
    readme_dir = os.path.dirname(readme_path)

    dada = None
    with open(readme_path) as f:
        dada = f.read()
    # strip old embeded dada
    dada = re.sub(r'source=\"data:image.*\"', "", dada)

    editor = QTextEdit()
    editor.setText(dada)
    editor.document().setMetaInformation(QTextDocument.DocumentUrl, readme_dir)
    html = editor.toHtml()

    image_elements = list()
    root = ElementTree.fromstring(html)
    for element in root.iter():
        for sub in element[:]:
            if sub.tag == "head":
                element.remove(sub)
        element.attrib.pop("style", None)
        if element.tag == "img":
            image_elements.append(element)

    width_scalar = calculate_width_scalar(readme_dir, image_elements, 577.0)

    for img in image_elements:
        rel_src_path = img.attrib.get("src", "")
        if rel_src_path.startswith("data:image"):
            print(
                "Embeded src. skipping! (readme_path={readme_path!r})".format(
                    **locals()))
            return
        if rel_src_path:
            src_path = os.path.abspath(os.path.join(readme_dir, rel_src_path))
            if not os.path.isfile(src_path):
                print(
                    "image file missing! (readme_dir={readme_dir!r}, src_path={src_path!r})"
                    .format(**locals()))
            else:
                rel_src_path = os.path.relpath(src_path, readme_dir)
                rel_src_path = "./" + rel_src_path.replace("\\", "/")
                # attribute order must be correct !!!
                # ElementTree in python<3.8 uses sort(dict.items())
                # sooo... lets make sure that there is alphaphetical dummy prefix...
                new_attributes = [
                    ("001_DEL_XML_SUCKS_src", rel_src_path),
                    ("002_DEL_XML_SUCKS_source",
                     embeded_image_scale_to_width(src_path,
                                                  width_scalar=width_scalar))
                ]
                new_attributes.extend((k, v)
                                      for k, v in sorted(img.attrib.items())
                                      if k not in {"src", "source"})
                img.attrib.clear()
                img.attrib.update(new_attributes)

    html = ElementTree.tostring(root).decode()
    html = re.sub(r"\d{3}_DEL_XML_SUCKS_", "", html)
    with open(readme_path, "wb") as f:
        f.write(html.encode("utf-8"))