Skip to content

Commit

Permalink
Blog jmespath emacs library (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
UnresolvedCold authored Feb 7, 2024
1 parent f5d13a2 commit bea258a
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 4 deletions.
13 changes: 9 additions & 4 deletions org-to-mdx.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@ def replace_image(match):

# Convert code block to md code block
elif org_line.startswith("#+BEGIN_SRC") or org_line.startswith("#+begin_src"):
md_content += "```" + org_line.split(" ")[1].split(" ")[0] + "\n"
if len(org_line.split(" ")) == 1:
md_content += "```" + "\n"
if len(org_line.split(" ")) > 1:
md_content += "```" + org_line.split(" ")[1].split(" ")[0] + "\n"
in_code_block = True
elif org_line.startswith("#+END_SRC") or org_line.startswith("#+end_src"):
md_content += "```\n"
Expand All @@ -87,6 +90,8 @@ def replace_image(match):
# images, contains .png, .jpg, .jpeg, .gif
elif re.search(r'\[\[([^\]]+)\]\]', org_line):
md_content += re.sub(r'\[\[([^\]]+)\]\]', replace_image, org_line) + "\n"
elif re.search(r'.*\[\[(.*?)\]\[(.*?)\]\].*', org_line):
md_content += re.sub(r"\[\[(.*?)\]\[(.*?)\]\]", lambda x: "[" + x.group(2) + "](" + x.group(1) + ")", org_line)
# Normal line
else:
# Bold
Expand All @@ -95,10 +100,10 @@ def replace_image(match):
org_line = re.sub(r"/(.*?)/", lambda x: "*" + x.group(1) + "*", org_line)
# Underline
org_line = re.sub(r"_(.*?)_", lambda x: "<u>" + x.group(1) + "</u>", org_line)
# Strikethrough
org_line = re.sub(r"~(.*?)~", lambda x: "~~" + x.group(1) + "~~", org_line)
# Inline code block
org_line = re.sub(r"~(.*?)~", lambda x: "`" + x.group(1) + "`", org_line)
# Link
org_line = re.sub(r"\[\[(.*?)\]\[(.*?)\]\]", lambda x: "[" + x.group(2) + "](" + x.group(1) + ")", org_line)
# org_line = re.sub(r"\[\[(.*?)\]\[(.*?)\]\]", lambda x: "[" + x.group(2) + "](" + x.group(1) + ")", org_line)
# Table
org_line = re.sub(r"\|", lambda x: " | ", org_line)
# Checkbox
Expand Down
62 changes: 62 additions & 0 deletions org/2024-02-06-jmespath-emacs-library.org
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#+title: Using Jmespath in Emacs
#+AUTHOR: Shubham Kumar
#+DATE: 2024-02-06
#+PROPERTY: description: Querying JSON files/data using Jmespath library in Emacs
#+PROPERTY: draft: false
#+PROPERTY: heroImage: /hero-images/default.png
#+PROPERTY: ideaDate: Feb 06, 2024

* Introduction

When you have a small JSON file, it is quite easy to look for what you want.
But querying a large JSON data is very troublesome.

This is where people use tools like Jmespath to filter and transform data into their liking.
This post is about a small wrapper over `jp` CLI utility that you can use while working on JSON files in Emacs.

* Installing JP

** Linux

On Linux, you can install the utility by first downloding the binary and then installing it.
#+begin_src bash
sudo wget https://github.com/jmespath/jp/releases/latest/download/jp-linux-amd64 \
-O /usr/local/bin/jp && sudo chmod +x /usr/local/bin/jp
#+end_src

** Mac
On Mac, you can install Jmespath CLI using `brew`.
#+begin_src bash
brew install jmespath/jmespath/jp
#+end_src

* Adding Jmespath recipe in Emacs

This is my first recipe that I published on [[https://melpa.org/][MELPA]].
Here are the steps to install it in ~Doom~ using ~straight~.
You can use any package manager to install it using ~MELPA~ repository.

** Doom Emacs

1. On Doom, you can just mention the below recipe in your ~package.el~.
#+begin_src elisp
(package! jmespath)
#+end_src

2. Also, add the below line in your ~config.el~.
#+begin_src elisp
(use-package jmespath)
#+end_src

* Using Jmespath

There is an interactive function, ~jmespath-query-and-show~ that you can use to query the currently opened buffer or a file.
To use it with current buffer, you can simply call this function and enter your query.
The output will be shown on a new buffer with name *JMESPath Result*.

To use this with a different file, you can set the ~Universal Argument~ using ~C-u~ or ~SPC-u~ (using evil).
Then you can enter the file to execute the query on.

* Read more

To learn more about JMESPath, visit [[https://jmespath.org/][Jamespath page]].
64 changes: 64 additions & 0 deletions src/content/blog/2024-02-06-jmespath-emacs-library.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
---
title: Using Jmespath in Emacs
AUTHOR: Shubham Kumar
DATE: 2024-02-06
description: Querying JSON files/data using Jmespath library in Emacs
draft: false
heroImage: /hero-images/default.png
ideaDate: Feb 06, 2024
---


## Introduction

When you have a small JSON file, it is quite easy to look for what you want.
But querying a large JSON data is very troublesome.

This is where people use tools like Jmespath to filter and transform data into their liking.
This post is about a small wrapper over `jp` CLI utility that you can use while working on JSON files in Emacs.

## Installing JP

### Linux

On Linux, you can install the utility by first downloding the binary and then installing it.
```bash
> sudo wget https://github.com/jmespath/jp/releases/latest/download/jp-linux-amd64 \
> -O /usr/local/bin/jp && sudo chmod +x /usr/local/bin/jp
```

### Mac
On Mac, you can install Jmespath CLI using `brew`.
```bash
> brew install jmespath/jmespath/jp
```

## Adding Jmespath recipe in Emacs

This is my first recipe that I published on [MELPA](https://melpa.org/).Here are the steps to install it in `Doom` using `straight`.
You can use any package manager to install it using `MELPA` repository.

### Doom Emacs

1. On Doom, you can just mention the below recipe in your `package.el`.
```elisp
> (package! jmespath)
```

2. Also, add the below line in your `config.el`.
```elisp
> (use-package jmespath)
```

## Using Jmespath

There is an interactive function, `jmespath-query-and-show` that you can use to query the currently opened buffer or a file.
To use it with current buffer, you can simply call this function and enter your query.
The output will be shown on a new buffer with name **JMESPath Result**.

To use this with a different file, you can set the `Universal Argument` using `C-u` or `SPC-u` (using evil).
Then you can enter the file to execute the query on.

## Read more

To learn more about JMESPath, visit [Jamespath page](https://jmespath.org/).

0 comments on commit bea258a

Please sign in to comment.