• crankygeologist

    @cvp Thank you, so much. It works! Now to study your changes. ;-)

    posted in Pythonista read more
  • crankygeologist

    I’m curious to know if Pythonista for iOS supports Seth Brown’s python script within the “md - flip links” workflow, posted at:


    I copied the python script out of the macOS workflow, saved it within Pythonista, and enabled it as a share extension shortcut. When I select the markdown text in iAWriter and launch the shortcut, nothing happens. What other tweaks do I need to make?


    Here’s the code:

    # encoding=utf8
    Seth Brown
    from sys import stdin, stdout
    import argparse
    import re
    import appex
    from collections import OrderedDict
    class ForMd(object):
      """Format mardown text"""
        text = appex.get_text()
        if not text:
            print('No input text found. Use this script from the share sheet in an app like Notes.')
      def __init__(self, text):
        super(ForMd, self).__init__()
        self.text = text
        self.match_links = re.compile(r'(\[.*?\])\s?(\[.*?\]|\(.*?\))',
                                      re.DOTALL | re.MULTILINE)
        self.match_refs = re.compile(r'(?<=\n)\[.*?\]:\s?.*')
        self.data = []
      def _links(
          self, ):
        """find Markdown links"""
        links = re.findall(self.match_links, self.text)
        for link in links:
          # remove newline breaks from urls spanning multi-lines
          parsed_link = [s.replace('\n', '') for s in link]
          yield parsed_link
      def _refs(self):
        """find Markdown references"""
        refs = re.findall(self.match_refs, self.text)
        refs = OrderedDict(i.split(":", 1) for i in refs)
        return refs
      def _format(self):
        """process text"""
        links = (i for i in self._links())
        refs = self._refs()
        for n, link in enumerate(links):
          text, ref = link
          ref_num = ''.join(("[", str(n + 1), "]: "))
          if ref in refs.keys():
            url = refs.get(ref).strip()
            formd_ref = ''.join((ref_num, url))
            formd_text = ''.join((text, ref_num))
            self.data.append([formd_text, formd_ref])
          elif text in refs.keys():
            url = refs.get(text).strip()
            formd_ref = ''.join((ref_num, url))
            formd_text = ''.join((text, ref_num))
            self.data.append([formd_text, formd_ref])
          elif ref not in refs.keys():
            parse_ref = ref.strip("()")
            formd_ref = ''.join((ref_num, parse_ref))
            formd_text = ''.join((text, ref_num))
            self.data.append([formd_text, formd_ref])
      def inline_md(self):
        """generate inline markdown """
        text_link = iter([
          ''.join((_[0].split("][", 1)[0], "](", _[1].split(":", 1)[1].strip(),
                   ")")) for _ in self.data
        formd_text = self.match_links.sub(lambda _: next(text_link), md)
        formd_md = self.match_refs.sub('', formd_text).strip()
        yield formd_md
      def ref_md(self):
        """generate referenced markdown"""
        ref_nums = iter([_[0].rstrip(" :") for _ in self.data])
        formd_text = self.match_links.sub(lambda _: next(ref_nums), md)
        formd_refs = self.match_refs.sub('', formd_text).strip()
        references = (i[1] for i in self.data)
        formd_md = '\n'.join((formd_refs, '\n', '\n'.join(i for i in references)))
        yield formd_md
      def flip(self):
        """convert markdown to the opposite style of the first text link"""
        first_match = re.search(self.match_links, self.text).group(0)
        if '(' and ')' in first_match:
          formd_md = self.ref_md()
          formd_md = self.inline_md()
        return formd_md
    if __name__ == '__main__':
      description = 'formd: A (for)matting (M)ark(d)own tool.'
      p = argparse.ArgumentParser(description=description)
        help="convert text to referenced Markdown",
        help="convert text to inline Markdown",
        help="convert to opposite style Markdown",
      args = p.parse_args()
      md = stdin.read()
      text = ForMd(md)
      [stdout.write(t) for t in text.flip()]

    posted in Pythonista read more

Internal error.

Oops! Looks like something went wrong!