Skip to content

Commit

Permalink
✨ feat(remote_text shortcode): support line ranges (#399)
Browse files Browse the repository at this point in the history
  • Loading branch information
welpo committed Sep 22, 2024
1 parent 90c1da1 commit 008b976
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 1 deletion.
14 changes: 14 additions & 0 deletions content/blog/shortcodes/index.ca.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,14 @@ dist/

Afegeix text des d'una URL remota o un arxiu local.

El shortcode accepta tres paràmetres:

- `src`: L'URL d'origen o ruta del fitxer (obligatori)
- `start`: Primera línia a mostrar (opcional, comença a 1)
- `end`: Número de l'última línia (opcional, per defecte és 0, l'última línia)

{{ admonition(type="info", text="`start` i `end` són inclusius. `start=3, end=3` mostrarà només la tercera línia.") }}

**Important**:

- **Arxius remots VS arxius locals**: Si `src` comença amb "http", es tractarà com un arxiu remot. D'altra banda, s'assumeix que és una ruta d'arxiu local.
Expand All @@ -229,6 +237,12 @@ Mostra el text d'un arxiu local:
{{/* remote_text(src="ruta/a/arxiu.txt") */}}
```

Mostreu només les línies 3 a 5 d'un arxiu local:

```
{{/* remote_text(src="ruta/a/arxiu.txt", start=3, end=5) */}}
```

### Advertències

Destaca informació amb aquests shortcodes. Hi ha cinc tipus (`type`): `note`, `tip`, `info`, `warning`, i `danger`.
Expand Down
14 changes: 14 additions & 0 deletions content/blog/shortcodes/index.es.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,14 @@ dist/

Añade texto desde una URL remota o un archivo local.

El shortcode acepta tres parámetros:

- `src`: La URL de origen o ruta del archivo (obligatorio)
- `start`: Primera línea a mostrar (opcional, empieza en 1)
- `end`: Número de la última línea (opcional, por defecto es 0, la última línea)

{{ admonition(type="info", text="`start` y `end` son inclusivos. `start=3, end=3` mostrará solo la tercera línea.") }}

**Importante**:

- **Archivos remotos VS archivos locales**: Si `src` empieza con "http", se tratará como un archivo remoto. De lo contrario, se asume que es una ruta de archivo local.
Expand All @@ -230,6 +238,12 @@ Visualización de texto de un archivo local:
{{/* remote_text(src="ruta/a/archivo.txt") */}}
```

Mostar sólo las líneas 3 a 5 de un archivo remoto:

```
{{/* remote_text(src="https://example.com/script.py", start=3, end=5) */}}
```

### Advertencias

Destaca información con estos shortcodes. Hay cinco tipos (`type`): `note`, `tip`, `info`, `warning`, y `danger`.
Expand Down
14 changes: 14 additions & 0 deletions content/blog/shortcodes/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,14 @@ dist/

Embed text from a remote URL or a local file. To display the path or URL on the code block, see the [show source or path shortcode](#show-source-or-path).

The shortcode accepts three parameters:

- `src`: The source URL or file path (required)
- `start`: First line to display (optional, starts at 1)
- `end`: The ending line number (optional, defaults to 0, meaning the last line)

{{ admonition(type="info", text="`start` and `end` are inclusive. `start=3, end=3` will display only the third line.") }}

**Important**:

- **Remote VS local files**: If `src` starts with "http", it will be treated as a remote file. Otherwise, it assumes a local file path.
Expand All @@ -229,6 +237,12 @@ Displaying text from a local file:
{{/* remote_text(src="path/to/file.txt") */}}
```

Display lines 3 to 7 (both inclusive) of a local file:

```
{{/* remote_text(src="path/to/file.txt", start=3, end=7) */}}
```

### Admonitions

Bring attention to information with these admonition shortcodes. They come in five `type`s: `note`, `tip`, `info`, `warning`, and `danger`.
Expand Down
18 changes: 17 additions & 1 deletion templates/shortcodes/remote_text.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{%- set start = start | default(value=1) -%}
{%- set end = end | default(value=0) -%}

{#- load_data uses different arguments based on whether it's a remote or local file -#}
{%- if src is starting_with("http") -%}
{%- set response = load_data(url=src, format="plain") -%}
Expand All @@ -11,4 +14,17 @@
{%- set response = load_data(path=src, format="plain") -%}
{%- endif -%}
{%- endif -%}
{{- response | trim_end | safe -}}

{%- set lines = response | trim_end | split(pat="\n") -%}

{%- if start > 0 -%}
{%- set start = start - 1 -%}
{%- endif -%}

{%- if end == 0 or end > lines | length -%}
{%- set end = lines | length -%}
{%- endif -%}

{%- set lines = lines | slice(start=start, end=end) -%}

{{- lines | join(sep="\n") | safe -}}

0 comments on commit 008b976

Please sign in to comment.