Fix headline tags, table pretty printing and multiline links

- we can't just look at the len of the string (~ #bytes) - that breaks down for
  tables containing characters consisting of multiple bytes. This handles
  more (still not all) cases and is good enough for now
- add _ to allowed tag chars - also require space between headline and tags
- links (link itself, not the description) spanning multiple lines are not
  supported - otherwise we would have to take care of splitting link and adding
  indentation for org pretty printing - and that sounds like such an edge case
  that it seems cleaner to forbid them
This commit is contained in:
Niklas Fasching 2018-12-19 12:58:07 +01:00
parent fb837e04af
commit ec895cbe83
7 changed files with 61 additions and 4 deletions

View file

@ -17,7 +17,7 @@ type Headline struct {
} }
var headlineRegexp = regexp.MustCompile(`^([*]+)\s+(.*)`) var headlineRegexp = regexp.MustCompile(`^([*]+)\s+(.*)`)
var tagRegexp = regexp.MustCompile(`(.*?)\s*(:[A-Za-z0-9@#%:]+:\s*$)`) var tagRegexp = regexp.MustCompile(`(.*?)\s+(:[A-Za-z0-9_@#%:]+:\s*$)`)
func lexHeadline(line string) (token, bool) { func lexHeadline(line string) (token, bool) {
if m := headlineRegexp.FindStringSubmatch(line); m != nil { if m := headlineRegexp.FindStringSubmatch(line); m != nil {

View file

@ -210,6 +210,9 @@ func (d *Document) parseRegularLink(input string, start int) (int, Node) {
if len(rawLinkParts) == 2 { if len(rawLinkParts) == 2 {
link, description = rawLinkParts[0], d.parseInline(rawLinkParts[1]) link, description = rawLinkParts[0], d.parseInline(rawLinkParts[1])
} }
if strings.ContainsRune(link, '\n') {
return 0, nil
}
consumed := end + 2 consumed := end + 2
protocol, linkParts := "", strings.SplitN(link, ":", 2) protocol, linkParts := "", strings.SplitN(link, ":", 2)
if len(linkParts) == 2 { if len(linkParts) == 2 {

View file

@ -3,6 +3,7 @@ package org
import ( import (
"fmt" "fmt"
"strings" "strings"
"unicode/utf8"
) )
type stringBuilder = strings.Builder type stringBuilder = strings.Builder
@ -268,7 +269,7 @@ func (w *OrgWriter) writeTable(t Table) {
if content == "" { if content == "" {
content = " " content = " "
} }
n := column.Len - len(content) n := column.Len - utf8.RuneCountInString(content)
if n < 0 { if n < 0 {
n = 0 n = 0
} }

View file

@ -4,6 +4,7 @@ import (
"regexp" "regexp"
"strconv" "strconv"
"strings" "strings"
"unicode/utf8"
) )
type Table struct { type Table struct {
@ -89,8 +90,8 @@ func getColumnInfos(rows [][]string) []ColumnInfo {
continue continue
} }
if len(columns[i]) > columnInfos[i].Len { if n := utf8.RuneCountInString(columns[i]); n > columnInfos[i].Len {
columnInfos[i].Len = len(columns[i]) columnInfos[i].Len = n
} }
if m := columnAlignRegexp.FindStringSubmatch(columns[i]); m != nil && isSpecialRow(columns) { if m := columnAlignRegexp.FindStringSubmatch(columns[i]); m != nil && isSpecialRow(columns) {

View file

@ -42,6 +42,42 @@ table with separator after header
</figure> </figure>
<figure> <figure>
<table> <table>
<thead>
<tr>
<th>Character</th>
<th>Org</th>
<th>Rendered HTML</th>
</tr>
</thead>
<tbody>
<tr>
<td>Hyphen</td>
<td><code class="verbatim">a - b</code></td>
<td>a - b</td>
</tr>
<tr>
<td>Ndash</td>
<td><code class="verbatim">a -- b</code></td>
<td>a b</td>
</tr>
<tr>
<td>Mdash</td>
<td><code class="verbatim">a --- b</code></td>
<td>a — b</td>
</tr>
<tr>
<td>Ellipsis</td>
<td><code class="verbatim">a ... b</code></td>
<td>a … b</td>
</tr>
</tbody>
</table>
<figcaption>
table with unicode characters
</figcaption>
</figure>
<figure>
<table>
<tbody> <tbody>
<tr> <tr>
<td class="align-right">1</td> <td class="align-right">1</td>

View file

@ -9,6 +9,14 @@
|---+---+---| |---+---+---|
| 1 | 2 | 3 | | 1 | 2 | 3 |
#+CAPTION: table with unicode characters
| Character | Org | Rendered HTML |
|-----------+-----------+---------------|
| Hyphen | =a - b= | a - b |
| Ndash | =a -- b= | a b |
| Mdash | =a --- b= | a — b |
| Ellipsis | =a ... b= | a … b |
#+CAPTION: table without header (but separator before) #+CAPTION: table without header (but separator before)
|---+---+---| |---+---+---|
| 1 | 2 | 3 | | 1 | 2 | 3 |

View file

@ -9,6 +9,14 @@
|---+---+---| |---+---+---|
| 1 | 2 | 3 | | 1 | 2 | 3 |
#+CAPTION: table with unicode characters
| Character | Org | Rendered HTML |
|-----------+-----------+---------------|
| Hyphen | =a - b= | a - b |
| Ndash | =a -- b= | a b |
| Mdash | =a --- b= | a — b |
| Ellipsis | =a ... b= | a … b |
#+CAPTION: table without header (but separator before) #+CAPTION: table without header (but separator before)
|---+---+---| |---+---+---|
| 1 | 2 | 3 | | 1 | 2 | 3 |