Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replaced AsciiDoc headings with new style #18

Open
wants to merge 176 commits into
base: main
Choose a base branch
from

Conversation

mbrukman
Copy link
Contributor

Fixes #17.

As described in the Asciidoc best practices document:
http://asciidoctor.org/docs/asciidoc-recommended-practices/#section-titles
using heading markers using underlines of various characters is not recommended
for several reasons:

  • hard to remember which character means which level header
  • hard to maintain the correct number of characters (must match heading line)

This change takes the same approach as a similar change in JanusGraph:
JanusGraph/janusgraph@dc67ce7
with some minor modifications to handle Windows-style EOL \r\n in this file,
whereas the other change works only with UNIX-style EOL \n.

What that required was stripping just the Windows-style EOL characters before doing
the regex matches:

line = line.rstrip('\r\n')

Note that using line = line.rstrip() doesn't work here, because some lines
already end with a whitespace character, which leads to spurious diffs.

Then, we manually added them back via:

sys.stdout.write('%s\r\n' % ...)

Using print in this case would give us UNIX-style EOL, causing the entire file
to show a diff on every line.

Here's the entire file with modifications for this run, which may be useful in
the future if/when someone needs to reapply this to an AsciiDoc file formatted
with Windows EOL chars:

#!/usr/bin/python

import re
import sys

def main(argv):
    # http://asciidoctor.org/docs/asciidoc-recommended-practices/#section-titles
    patterns = [
        (re.compile('^=+$'), '='),
        (re.compile('^-+$'), '=='),
        (re.compile('^~+$'), '==='),
        (re.compile('^\^+$'), '===='),
        (re.compile('^\++$'), '====='),
    ]

    with open(argv[1], 'r') as input_file:
        prev_line = None
        curr_line = None
        for line in input_file.readlines():
            line = line.rstrip('\r\n')
            prev_line = curr_line
            curr_line = line

            if prev_line is None:
                continue

            for pattern, heading in patterns:
                if pattern.match(curr_line) and len(prev_line) == len(curr_line):
                    sys.stdout.write('%s %s\r\n' % (heading, prev_line))
                    prev_line = None
                    curr_line = None
                    break

            if prev_line is not None:
                sys.stdout.write('%s\r\n' % prev_line)

        # end for
        if curr_line is not None:
            sys.stdout.write(curr_line)

    # end with

if __name__ == '__main__':
    main(sys.argv)

krlawrence and others added 19 commits December 12, 2017 12:17
Fixes krlawrence#17.

As described in the Asciidoc best practices document:
http://asciidoctor.org/docs/asciidoc-recommended-practices/#section-titles
using heading markers using underlines of various characters is not recommended
for several reasons:

* hard to remember which character means which level header
* hard to maintain the correct number of characters (must match heading line)

This change takes the same approach as a similar change in JanusGraph:
JanusGraph/janusgraph@dc67ce7
with some minor modifications to handle Windows-style EOL `\r\n` in this file,
whereas the other change works only with UNIX-style EOL `\n`.

What that required was stripping just the Windows-style EOL characters before doing
the regex matches:

```
line = line.rstrip('\r\n')
```

Note that using `line = line.rstrip()` doesn't work here, because some lines
already end with a whitespace character, which leads to spurious diffs.

Then, we manually added them back via:

```
sys.stdout.write('%s\r\n' % ...)
```

Using `print` in this case would give us UNIX-style EOL, causing the entire file
to show a diff on every line.

Here's the entire file with modifications for this run, which may be useful in
the future if/when someone needs to reapply this to an AsciiDoc file formatted
with Windows EOL chars:

```
#!/usr/bin/python

import re
import sys

def main(argv):
    # http://asciidoctor.org/docs/asciidoc-recommended-practices/#section-titles
    patterns = [
        (re.compile('^=+$'), '='),
        (re.compile('^-+$'), '=='),
        (re.compile('^~+$'), '==='),
        (re.compile('^\^+$'), '===='),
        (re.compile('^\++$'), '====='),
    ]

    with open(argv[1], 'r') as input_file:
        prev_line = None
        curr_line = None
        for line in input_file.readlines():
            line = line.rstrip('\r\n')
            prev_line = curr_line
            curr_line = line

            if prev_line is None:
                continue

            for pattern, heading in patterns:
                if pattern.match(curr_line) and len(prev_line) == len(curr_line):
                    sys.stdout.write('%s %s\r\n' % (heading, prev_line))
                    prev_line = None
                    curr_line = None
                    break

            if prev_line is not None:
                sys.stdout.write('%s\r\n' % prev_line)

        # end for
        if curr_line is not None:
            sys.stdout.write(curr_line)

    # end with

if __name__ == '__main__':
    main(sys.argv)

```
@krlawrence krlawrence added this to High priority in Planning Aug 29, 2020
@krlawrence krlawrence moved this from High priority to Low priority in Planning Aug 30, 2020
@krlawrence krlawrence self-assigned this Aug 30, 2020
@krlawrence
Copy link
Owner

We just started work on a Second Edition of Practical Gremlin. As part of that we will update the entire tool chain and factor in these types of changes. I'm leaving this PR open as a reminder.

@krlawrence krlawrence moved this from Low priority to Full pass in Planning Oct 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Planning
  
Full pass
Development

Successfully merging this pull request may close these issues.

Replace AsciiDoc headings with new recommended style
3 participants