Exemplo n.º 1
0
    def head_lines(self):
        match = chapter_re.search(self.line)

        if match:
            type = match.group(1)
            text = match.group(2)
            tag = ArangoDB.clean_title(text).lower()

            if type == "BOOK":
                indent = "#"
            elif type == "CHAPTER":
                indent = "#"
            elif type == "SECTION":
                indent = "##"
            elif type == "SUBSECTION":
                indent = "###"
            elif type == "SUBSUBSECTION":
                indent = "####"

            anchorName = unescameMDInANames_re.sub("_", tag)
            anchor = '<a name=\"%s\"></a>\n' % anchorName
            header = '%s %s\n' % (indent, text)

            self.transforms.append(Transform(self.linenum, "swap", header))
            self.transforms.append(Transform(self.linenum, "prepend", anchor))

        return match
Exemplo n.º 2
0
    def transform(self, data):
        transforms = []
        linenum = 0
        in_block = False
        current_block = ""
        in_fenced_code_block = False

        for line in data:
            # Handling fenced code blocks (for Github-flavored markdown)
            if fencedcodere.search(line):
                if in_fenced_code_block:
                    in_fenced_code_block = False
                else:
                    in_fenced_code_block = True

            # Are we in a code block?
            if not in_fenced_code_block and not codere.search(line):
                # Is this line part of an existing LaTeX block?
                if in_block:
                    transforms.append(Transform(linenum, "drop"))
                    current_block += "\n" + line

                match = singlelinere.search(line)
                if match:
                    code_pos = []
                    for m in spancodere.finditer(line):
                        code_pos += range(*m.span())

                    if not (match.start(0) in code_pos
                            or match.end(0) in code_pos):

                        # Single LaTeX line
                        tex = match.group(0)
                        before_tex = line[0:line.find(tex)]
                        after_tex = line[(line.find(tex) + len(tex)):len(line)]
                        transforms.append(
                            Transform(
                                linenum, "swap",
                                before_tex + self.render(tex) + after_tex))
                else:
                    match = startorendre.search(line)
                    if match:
                        # Starting or ending a multi-line LaTeX block
                        if in_block:
                            # Ending a LaTeX block
                            transforms.pop()  # undo last drop
                            transforms.append(
                                Transform(linenum, "swap",
                                          self.render(current_block)))
                        else:
                            # Starting a LaTeX block
                            current_block = line
                            transforms.append(Transform(linenum, "drop"))
                        in_block = not in_block

            linenum += 1

        return transforms
Exemplo n.º 3
0
    def transform(self, data):
        transforms = []
        global CURRENT_BASE
        linenum = 0
        for line in data:

            branch = BRANCH.replace('origin/', '')

            # a little hack to make a !SUBMODULE directive look like an !INCLUDE directive!
            if line[0:10] == "!SUBMODULE":
                parts = line.strip().replace('\'', '').replace('"',
                                                               '').split(" ")
                if parts[1].startswith('http'):
                    CURRENT_BASE = parts[1]
                else:
                    CURRENT_BASE = GITBASE + parts[1]
                line = '!INCLUDE "' + BASE + parts[1].split(
                    '/')[-1] + '/' + parts[3] + '"'
                branch = parts[2]

            match = self.includere.search(line)
            if match:
                includedata = self.include(match, branch=branch)
                transform = Transform(linenum=linenum,
                                      oper="swap",
                                      data=includedata)
                transforms.append(transform)

            linenum += 1

        return transforms
Exemplo n.º 4
0
    def transform(self, data):
        transforms = []

        linenum = 0
        for line in data:

            # a little hack to make a !SUBMODULE directive look like an !INCLUDE directive!
            if line[0:10] == "!SUBMODULE":
                parts = line.strip().replace('\'', '').replace('"',
                                                               '').split(" ")
                line = '!INCLUDE "' + BASE + parts[1].split(
                    '/')[-1] + '/' + parts[3] + '"'

            match = self.includere.search(line)
            if match:
                includedata = self.include(match)

                transform = Transform(linenum=linenum,
                                      oper="swap",
                                      data=includedata)
                transforms.append(transform)

            linenum += 1

        return transforms
Exemplo n.º 5
0
    def transform(self, data):
        self.transforms = []
        self.linenum = 0
        self.dropExampleRun = False

        in_fenced_code_block = False

        for self.line in data:
            if self.dropExampleRun:
                match = end_example_run_re.search(self.line)

                if match:
                    self.dropExampleRun = False

                self.transforms.append(Transform(self.linenum, "drop"))

            else:

                # Handling fenced code blocks (for Github-flavored markdown)
                if fencedcodere.search(self.line):
                    if in_fenced_code_block:
                        in_fenced_code_block = False
                    else:
                        in_fenced_code_block = True

                # Are we in a code block?
                if not in_fenced_code_block and not codere.search(self.line):
                    done = self.head_lines()

                    if not done:
                        done = self.rest_example()

            self.linenum += 1

        return self.transforms
Exemplo n.º 6
0
    def transform(self, data):
        transforms = []

        linenum = 0
        for line in data:
            match = self.includere.search(line)
            if match:
                includedata = self.include(match)

                transform = Transform(linenum=linenum, oper="swap",
                                      data=includedata)
                transforms.append(transform)

            linenum += 1

        return transforms
Exemplo n.º 7
0
    def transform(self, data):
        transforms = []

        reffound = False
        reflines = []
        refdata = ""

        links = []

        # iterate through the document looking for !REF markers and links
        linenum = 0
        for line in data:

            match = refre.search(line)
            if match:
                reffound = True
                reflines.append(linenum)

            match = linkre.search(line)
            if match:
                name = match.group(1)
                if len(match.groups()) > 2:
                    title = match.group(3)
                else:
                    title = name.lower()

                links.append((name, title))

            linenum += 1

        # short circuit if no markers found
        if not reffound:
            return []

        for name, title in links:
            refdata += "*\t[%s][%s]\n" % (title, name)

        # create transforms for each marker
        for linenum in reflines:
            transforms.append(Transform(linenum, "swap", refdata))

        return transforms
Exemplo n.º 8
0
    def transform(self, data, path):
        transforms = []

        linenum = 0
        for line in data:
            match = self.includere.search(line)
            if match:
                if path:
                    #for s in reversed(path):
                    includedata = self.include_dir(match, path)
                else:
                    includedata = self.include(match)

                transform = Transform(linenum=linenum,
                                      oper="swap",
                                      data=includedata)
                transforms.append(transform)

            linenum += 1

        return transforms
Exemplo n.º 9
0
    def transform(self, data):
        transforms = []
        in_fenced_code_block = False
        linenum = 0

        for line in data:
            # Handling fenced code blocks (for Github-flavored markdown)
            if fencedcodere.search(line):
                if in_fenced_code_block:
                    in_fenced_code_block = False
                else:
                    in_fenced_code_block = True

            # Are we in a code block?
            if not in_fenced_code_block and not codere.search(line):
                match = youtube_url_re.search(line)
                if match:
                    # find URL of youtube video and screenshot
                    url = match.group(1)
                    image_url = 'http://img.youtube.com/vi/%s/0.jpg' % url
                    video_url = 'http://www.youtube.com/watch?v=%s' % url
                    processed_image_dir = os.path.join('images', 'youtube')
                    processed_image_path = os.path.join(processed_image_dir,
                                                        '%s.png' % url)

                    # do we already have a screenshot?
                    if not os.path.isfile(processed_image_path):
                        # create directories if needed
                        if not os.path.exists(processed_image_dir):
                            os.makedirs(processed_image_dir)

                        self._add_play_button(image_url, processed_image_path)

                    image_link = ('[![Link to Youtube video](%s)](%s)\n' %
                                  (processed_image_path, video_url))
                    transforms.append(Transform(linenum, "swap", image_link))

            linenum += 1

        return transforms
Exemplo n.º 10
0
    def transform(self, data):
        transforms = []

        lowestdepth = 10

        tocfound = False
        toclines = []
        tocdepth = 0
        tocdata = ""

        headers = {}

        infencedcodeblock = False

        # iterate through the document looking for markers and headers
        linenum = 0
        for line in data:

            # Fenced code blocks (Github-flavored markdown)
            match = fencedcodere.search(line)
            if match:
                if infencedcodeblock:
                    infencedcodeblock = False
                else:
                    infencedcodeblock = True

            # !TOC markers
            match = tocre.search(line)
            if match:
                tocfound = True
                depth = match.group(1)
                if depth is not None:
                    depth = int(depth)
                    tocdepth = max(depth, tocdepth)
                toclines.append(linenum)

            # hash headers
            match = atxre.search(line)
            if match and not infencedcodeblock:
                depth = len(match.group(1))
                title = match.group(2).strip()
                headers[linenum] = (depth, title)

                if tocfound:
                    lowestdepth = min(depth, lowestdepth)

            # underlined headers
            match = setextre.search(line)
            if match and not infencedcodeblock:
                depth = 1 if match.group(1)[0] == "=" else 2
                title = lastline.strip()
                headers[linenum - 1] = (depth, title)

                if tocfound:
                    lowestdepth = min(depth, lowestdepth)

            lastline = line
            linenum += 1

        # short circuit if no !TOC directive
        if not tocfound:
            return []

        if tocdepth == 0:
            tocdepth = 6

        stack = []
        headernum = 0

        lastdepth = 1
        depthoffset = 1 - lowestdepth

        keys = headers.keys()
        keys.sort()

        short_titles = []

        # interate through the list of headers, generating the nested table
        # of contents data, and creating the appropriate transforms
        for linenum in keys:
            if linenum < toclines[0]:
                continue

            (depth, title) = headers[linenum]
            depth += depthoffset
            short = re.sub("([\s,-,\(,\)]+)", "",
                           TableOfContents.clean_title(title)).lower()

            if short in short_titles:
                i = 1
                short_i = short
                while short_i in short_titles:
                    short_i = short + "-" + str(i)
                    i += 1
                short = short_i
            short_titles.append(short)

            while depth > lastdepth:
                stack.append(headernum)
                headernum = 0
                lastdepth += 1

            while depth < lastdepth:
                headernum = stack.pop()
                lastdepth -= 1

            headernum += 1

            if depth > tocdepth:
                continue

            if depth == 1:
                section = "%d\\. " % headernum
            else:
                section = ".".join([str(x)
                                    for x in stack]) + ".%d\\. " % headernum

            tocdata += "%s [%s](#%s)  \n" % (
                section, TableOfContents.clean_title(title), short)

            transforms.append(
                Transform(linenum, "swap",
                          data[linenum].replace(title, section + title)))
            transforms.append(
                Transform(linenum, "prepend",
                          "<a name=\"%s\"></a>\n\n" % short))

        # create transforms for the !TOC markers
        for linenum in toclines:
            transforms.append(Transform(linenum, "swap", tocdata))

        return transforms
Exemplo n.º 11
0
    def transform(self, data):
        transforms = []
        in_fenced_code_block = False
        linenum = 0

        for line in data:
            # Handling fenced code blocks (for Github-flavored markdown)
            if fencedcodere.search(line):
                if in_fenced_code_block:
                    in_fenced_code_block = False
                else:
                    in_fenced_code_block = True

            # Are we in a code block?
            if not in_fenced_code_block and not codere.search(line):
                match = youtube_url_re.search(line)
                if match:
                    # find URL of youtube video and screenshot
                    url = match.group(1)
                    image_url = 'http://img.youtube.com/vi/%s/0.jpg' % url
                    video_url = 'http://www.youtube.com/watch?v=%s' % url
                    processed_image_dir = os.path.join('images', 'youtube')
                    processed_image_path = os.path.join(processed_image_dir,
                                                        '%s.png' % url)

                    # do we already have a screenshot?
                    if not os.path.isfile(processed_image_path):
                        # try to add a play button to the screenshot
                        try:
                            import Image
                            from tempfile import NamedTemporaryFile

                            # create directories if needed
                            if not os.path.exists(processed_image_dir):
                                os.makedirs(processed_image_dir)

                            # create temporary files for image operations
                            screenshot_img = NamedTemporaryFile(suffix=".jpg")
                            button_img = NamedTemporaryFile(suffix=".png")

                            # grab screenshot and button image
                            urllib.urlretrieve(image_url, screenshot_img.name)
                            urllib.urlretrieve(play_button_url,
                                               button_img.name)

                            # layer the images using PIL and save
                            background = Image.open(screenshot_img.name)
                            foreground = Image.open(button_img.name)
                            background.paste(foreground, (90, 65), foreground)
                            background.save(processed_image_path)

                        except Exception as e:
                            print('Unable to add play button to YouTube '
                                  'screenshot (%s). Using the screenshot '
                                  'on its own instead.' % e)

                    image_link = ('[![Link to Youtube video](%s)](%s)\n' %
                                  (processed_image_path, video_url))
                    transforms.append(Transform(linenum, "swap", image_link))

            linenum += 1

        return transforms