Skip to content

Commit

Permalink
added -o flag #5
Browse files Browse the repository at this point in the history
  • Loading branch information
dennisangemi committed Feb 16, 2023
1 parent 42fa1a0 commit 9c34112
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 22 deletions.
36 changes: 29 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,39 @@
# 🔧 frictionless2md
A tool to convert frictionless metadata (datapackage.yaml or json) to md file that can be used as readme
A tool to convert frictionless metadata (datapackage.yaml or json) to markdown file (that can be used as README).

> You can obtain a README file like this https://github.com/chiaraadornetto/dati-tv-elezioni-ue-2019#readme
> You can obtain a markdown file like this https://github.com/chiaraadornetto/dati-tv-elezioni-ue-2019#readme
Currently you can launch `./script.sh` in a directory where datapackage is located (make sure to copy `template.md`).
Read [requirements.txt](requirements.txt) otherwise the bash script won't work.
## Usage
Read [requirements.txt](requirements.txt) otherwise the bash script won't work.

Currently you can launch
```bash
./script.sh
```
in a directory where a datapackage is located (make sure to copy `template.md`) and the default output file will be `METADATA.md`.

### Options

**Set custom output filename**

If you want to use a custom output filename you can use `-o` flag:
```bash
./script.sh -o customfilename.md
```

## Development
`script.sh` is a bash script

---
<details>
<summary>Future developments</summary>

# Future developments
## Installation

You can install this tool
```bash
apt-get bla bla instal bla bla frictionless2md
```

## Usage

Launch simply `frictionless2md` in the same directory where `datapackage.yaml` is located.
Expand All @@ -25,7 +46,7 @@ Launch simply `frictionless2md` in the same directory where `datapackage.yaml` i
frictionless2md -o README.md
```

`-f README.md` declare the output filename
`-o README.md` declare the output filename

### Specify GitHub infos

Expand All @@ -39,3 +60,4 @@ Example (get the datapackage from dennisangemi/myrepo and convert to markdown):
```bash
frictionless2md -u dennisangemi myrepo -f README.md
```
</details>
6 changes: 4 additions & 2 deletions metadata.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,20 @@ Analisi della copertura delle elezioni europee del 2019 nella televisione italia
## Repository structure
```
.
├── METADATA.md
├── README.md
├── data
│   ├── dialect.json
│   └── trasmissioni.csv
├── datapackage.json
├── datapackage.yaml
├── diary.md
├── metadata.md
├── imieimetadatai.md
├── requirements.txt
├── script.sh
├── template.md
└── test.json
├── test.json
└── test.sh
```

## Data Dictionary
Expand Down
48 changes: 35 additions & 13 deletions script.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,30 @@

set -e

# set output filename
OUTPUT_FILENAME="metadata.md"
# set default output filename
DEFAULT_OUTPUT_FILENAME="METADATA.md"

### OPTIONS

# parsing
while getopts "o:" arg; do
case $arg in
o)
output_file=$OPTARG
;;
*)
echo -e "❌ \e[31mError: option not found\e[0m"
echo "Usage: ./script.sh -o FILENAME.md"
exit 1
;;
esac
done

# if output file is not set, use default
if [ -z $output_file ]; then
output_file=$DEFAULT_OUTPUT_FILENAME
# echo "Using default output filename: $output_file"
fi

### CHECK IF DATAPACKAGE EXISTS

Expand Down Expand Up @@ -33,30 +55,30 @@ fi
echo "✅ datapackage exists"

# copy template (to be modified)
cp template.md $OUTPUT_FILENAME
cp template.md $output_file

### PACKAGE TITLE
# add title
perl -i -p -e 's/{{{title}}}/'"$(cat datapackage.json | jq -r '.title')"'/g' $OUTPUT_FILENAME
perl -i -p -e 's/{{{title}}}/'"$(cat datapackage.json | jq -r '.title')"'/g' $output_file

### PACKAGE DESCRIPTION
# add repo description
perl -i -p -e 's/{{{repository-description}}}/'"$(cat datapackage.json | jq -r '.description')"'/g' $OUTPUT_FILENAME
perl -i -p -e 's/{{{repository-description}}}/'"$(cat datapackage.json | jq -r '.description')"'/g' $output_file

### REPORITORY STRUCTURE
# add tree
perl -i -p -e 's/{{{repository-structure}}}/'"$(tree | head -n -2)"'/g' $OUTPUT_FILENAME
perl -i -p -e 's/{{{repository-structure}}}/'"$(tree | head -n -2)"'/g' $output_file

### PACKAGE LICENSE
# add license info
sed -i "s|{{{license}}}|$(echo "This work is licensed under a ["$(cat datapackage.json | jq -r '.licenses[0].title') "]($(cat datapackage.json | jq -r '.licenses[0].path')) ("$(cat datapackage.json | jq -r '.licenses[0].name')") License")|g" $OUTPUT_FILENAME
sed -i "s|{{{license}}}|$(echo "This work is licensed under a ["$(cat datapackage.json | jq -r '.licenses[0].title') "]($(cat datapackage.json | jq -r '.licenses[0].path')) ("$(cat datapackage.json | jq -r '.licenses[0].name')") License")|g" $output_file

### CONTRIBUTORS
# add contributors table
# contributors_table=$(cat datapackage.json | jq '[.contributors[] | {Name: .title, Role: .role, Email: .email}]' | mlr --j2m cat)
# perl -i -p -e 's/{{{contributors}}}/'"$contributors_table"'/g' $OUTPUT_FILENAME
# perl -i -p -e 's/{{{contributors}}}/'"$contributors_table"'/g' $output_file
cat datapackage.json | jq '[.contributors[] | {Name: .title, Role: .role, Email: .email}]' | mlr --j2m cat > frct-contributors.md
sed -i -e '/{{{contributors}}}/r frct-contributors.md' -e '//d' metadata.md
sed -i -e '/{{{contributors}}}/r frct-contributors.md' -e '//d' $output_file

# if frct-contributors.md exists, delete it
if [ -f frct-contributors.md ]; then
Expand Down Expand Up @@ -100,8 +122,8 @@ do
delimiter=$(jq -r '.resources['$i'].dialect.csv.delimiter' datapackage.json)
echo "- Delimiter: \`$delimiter\`" >> dictionary.md
else
echo -e "⚠️ Warning: Dialect key not found for $filename"
echo "Delimiter info will not be added to $OUTPUT_FILENAME"
echo -e "⚠️ Warning: Dialect key not found for $filename"
echo "Delimiter info will not be added to $output_file"
fi

echo "- Encoding: \`$(cat datapackage.json | jq -r '.resources['$i'].encoding')\`" >> dictionary.md
Expand All @@ -123,7 +145,7 @@ do
done

# substitute {{{data-dictionary}}} with dictionary.md
sed -i -e '/{{{data-dictionary}}}/r dictionary.md' -e '//d' $OUTPUT_FILENAME
sed -i -e '/{{{data-dictionary}}}/r dictionary.md' -e '//d' $output_file

### CLEANUP

Expand All @@ -137,4 +159,4 @@ fi

### END

echo "$OUTPUT_FILENAME created"
echo "$output_file created"

0 comments on commit 9c34112

Please sign in to comment.