Skip to content

Commit

Permalink
Merge pull request #55 from jygastaud/52-asciidoc-support
Browse files Browse the repository at this point in the history
#52 - Add Asciidoc support
  • Loading branch information
phodal authored Aug 23, 2024
2 parents 8e4a281 + ad4f765 commit f92758c
Show file tree
Hide file tree
Showing 36 changed files with 1,898 additions and 1,022 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ indent_style = space
indent_size = 2
trim_trailing_whitespace = true
insert_final_newline = true
[*.md]
[*.md, *adoc, *.asciidoc]
trim_trailing_whitespace = false
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,5 @@ export.html
export.json
export.csv
export.md
export.adoc
architecture/
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.

=== [Unreleased]

* Add support of Asciidoc

### [1.4.5](https://github.com/phodal/adr/compare/v1.4.2...v1.4.5) (2023-05-19)


Expand Down
95 changes: 48 additions & 47 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ ADR - Architecture Decision Records(轻量级架构决策记录工具)

> ADR - Architecture Decision Records(轻量级架构决策记录工具)
[![Build Status](https://travis-ci.org/phodal/adr.svg?branch=master)](https://travis-ci.org/phodal/adr) [![codecov.io](https://codecov.io/github/phodal/adr/coverage.svg?branch=master)](https://codecov.io/github/phodal/adr?branch=master) [![Maintainability](https://api.codeclimate.com/v1/badges/5cd05f9857e0a2031ba1/maintainability)](https://codeclimate.com/github/phodal/adr/maintainability)
[![Build Status](https://travis-ci.org/phodal/adr.svg?branch=master)](https://travis-ci.org/phodal/adr) [![codecov.io](https://codecov.io/github/phodal/adr/coverage.svg?branch=master)](https://codecov.io/github/phodal/adr?branch=master) [![Maintainability](https://api.codeclimate.com/v1/badges/5cd05f9857e0a2031ba1/maintainability)](https://codeclimate.com/github/phodal/adr/maintainability)

[![Markdown Improve](https://img.shields.io/badge/markdown--improve-Phodal-blue.svg)](https://github.com/phodal/markdown-improve) [![node](https://img.shields.io/node/v/adr.svg)](https://www.npmjs.com/package/adr) [![npm](https://img.shields.io/npm/v/adr.svg)](https://www.npmjs.com/adr)

Expand All @@ -17,37 +17,37 @@ A good material about [Architecture decision record](https://github.com/joelpark

**功能特性**

- 支持 Windows, GNU/Linux, Mac OS
- Markdown 目录生成
- 为项目经理、业务人员等非开发人员,生成报表:HTML,CSV, JSON
- 兼容 adr-tools
- 国际化支持:English、中文、Brazilian Portuguese (pt-br), Standard Italian (it-IT)
- 状态历史
- 状态查询
- 更好的列表展示
- 兼容 adr-tools
- 支持 Windows, GNU/Linux, Mac OS
- Markdown 目录生成
- 为项目经理、业务人员等非开发人员,生成报表:HTML,CSV, JSON
- 兼容 adr-tools
- 国际化支持:English、中文、Brazilian Portuguese (pt-br), Standard Italian (it-IT)
- 状态历史
- 状态查询
- 更好的列表展示
- 兼容 adr-tools

[查看本项目的架构决策记录](/docs/adr)

**Features**

- Supported Windows, GNU/Linux, Mac OS
- **report for PM, BA**: html, csv, json
- generate markdown toc(see in [docs/adr](/docs/adr)
- **i18n**: English (en), 中文(zh-cn), Brazilian Portuguese (pt-br), Standard Italian (it-IT), Farsi (fa), French (fr)
- status logs
- status query
- better list view
- compatible adr-tools
- custom templates: add a `template.md` file in the save path

HTML Reporter Example
---
- [ADR Example](https://phodal.github.io/adr/examples/export-1.html)
- [ADR Tools Example](https://phodal.github.io/adr/examples/export-3.html)
- [会分期 Example](https://phodal.github.io/adr/examples/export-2.html)
- [Arachne Framework Example](https://phodal.github.io/adr/examples/export-4.html)
- Supported Windows, GNU/Linux, Mac OS
- **report for PM, BA**: html, csv, json
- Support Markdown and Asciidoc
- generate markdown and asciidoc toc(see in [docs/adr](/docs/adr)
- **i18n**: English (en), 中文(zh-cn), Brazilian Portuguese (pt-br), Standard Italian (it-IT), Farsi (fa), French (fr)
- status logs
- status query
- better list view
- compatible adr-tools
- custom templates: add a `template.md` (or `template.adoc`, `template.asciidoc`) file in the save path

## HTML Reporter Example

- [ADR Example](https://phodal.github.io/adr/examples/export-1.html)
- [ADR Tools Example](https://phodal.github.io/adr/examples/export-3.html)
- [会分期 Example](https://phodal.github.io/adr/examples/export-2.html)
- [Arachne Framework Example](https://phodal.github.io/adr/examples/export-4.html)

Screenshots
---
Expand All @@ -56,22 +56,21 @@ Screenshots

![List Examples](docs/list-example.png)

### ADR Reporter
### ADR Reporter

![Reporter Examples](docs/reporter-example.png)

Install
---
## Install

1. install

```
```bash
npm install -g adr
```

2. init

```
```bash
adr init <language>
```

Expand Down Expand Up @@ -125,7 +124,7 @@ results:

### generate graph

```
```sh
adr generate graph
```

Expand All @@ -144,13 +143,13 @@ digraph {

### update filename by title

```
```bash
adr update
```

### decisions change logs

```
```bash
adr logs <index>
```

Expand All @@ -170,7 +169,7 @@ e.x. ``adr logs 9``

support: json, csv, html, markdown

```
```bash
adr export <type>
```

Expand All @@ -185,7 +184,7 @@ Index, 决策, 上次修改时间, 最后状态

### search adr

```
```bash
adr search <keyword>
```

Expand All @@ -205,24 +204,26 @@ Config
---

current:

- **language**, language
- **path**, save path
- **digits**, the index length, e.x. digits:3 001-index.md
- **prefix**, the prefix of files, e.x. adr-0001
- **editor**, the editor to open file, e.x. code, [more information](https://github.com/lahmatiy/open-in-editor#editor), and you can also use the editor by setting the program path, such as `/System/Applications/TextEdit.app/Contents/MacOS/TextEdit`
- **force_nfc**, whether to normalize the names of files generated by `adr` commands in NFC (Normalization Form Canonical Composition) format, e.x. `true`

example config:
- **language**, language
- **path**, save path
- **digits**, the index length, e.x. digits:3 001-index.md
- **prefix**, the prefix of files, e.x. adr-0001
- **editor**, the editor to open file, e.x. code, [more information](https://github.com/lahmatiy/open-in-editor#editor), and you can also use the editor by setting the program path, such as `/System/Applications/TextEdit.app/Contents/MacOS/TextEdit`
- **force_nfc**, whether to normalize the names of files generated by `adr` commands in NFC (Normalization Form Canonical Composition) format, e.x. `true`
- **extension**, the document extension/format you want to used. `md` for Markdown (default value) or`adoc` for asciidoc

```
example config:

```json
{
"path":"doc/adr/",
"language":"zh-cn",
"prefix": "",
"digits": 4,
"editor": "code",
"force_nfc": true
"force_nfc": true,
"extension": "md"
}
```

Expand Down
4 changes: 2 additions & 2 deletions docs/adr/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Architecture Decision Records
# 架构决策记录

* [1. 编写完整的单元测试](0001-编写完整的单元测试.md)
* [2. 添加目录生成](0002-添加目录生成.md)
Expand All @@ -23,4 +23,4 @@
* [21. 思考更好的-adr-格式](0021-思考更好的-adr-格式.md)
* [22. 考虑添加索引前缀等参数来匹配一些额外的情况](0022-考虑添加索引前缀等参数来匹配一些额外的情况.md)
* [23. 可以使用命令行修改状态](0023-可以使用命令行修改状态.md)
* [24. 支持自定义模板路径](0024-支持自定义模板路径.md)
* [24. 支持自定义模板路径](0024-支持自定义模板路径.md)
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@
]
},
"dependencies": {
"@asciidoctor/core": "^3.0.4",
"colors": "^1.3.3",
"commander": "^5.1.0",
"find-in-files": "^0.5.0",
Expand Down
10 changes: 8 additions & 2 deletions src/lib/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ let DEFAULT_CONFIG = {
prefix: '',
digits: 4,
editor: 'code',
force_nfc: false
force_nfc: false,
extension: 'md'
}

function getAllConfig (defaultValue: string) {
Expand Down Expand Up @@ -79,6 +80,10 @@ function getForceNfc (): string {
return getConfig('force_nfc')
}

function getDocExtension (): string {
return getConfig('extension')
}

let Config = {
getAllConfig: getAllConfig,
getSavePath: getSavePath,
Expand All @@ -89,7 +94,8 @@ let Config = {
getConfig: getConfig,
getAssetsPath: getAssetsPath,
getCachePath: getCachePath,
getForceNfc: getForceNfc
getForceNfc: getForceNfc,
getDocExtension: getDocExtension
}

export default Config
63 changes: 58 additions & 5 deletions src/lib/StatusHelper.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import * as console from 'node:console'

let fs = require('fs')
let md = require('markdown').markdown
let asciidoctor = require('@asciidoctor/core')()

import Config from './Config'
import Utils from './utils'

let i18n = Utils.getI18n()
Expand All @@ -23,6 +27,28 @@ function getStatusSection (tree: any) {
return statusSection
}

function getAsciidocStatusSection (tree: any) {
let statusSection: string[] = []

tree.findBy({ 'section': 'section' }, function (section) {
if (section.getLevel() === 1 && section.getTitle() === i18n.Status) {
let content: string[] = []

section.blocks.forEach((ele) => {
if (ele.lines && ele.lines.length > 0) {
content.push(...ele.lines.filter(line => line && line.trim() !== ''))
}
})

if (content.length > 0) {
statusSection = content // Assigning the content to statusSection
}
}
})

return statusSection
}

function getStatusWithDate (statusSections: string[]) {
let status: string[] = []
for (let i = 0; i < statusSections.length; i++) {
Expand All @@ -39,20 +65,38 @@ function getStatusWithDate (statusSections: string[]) {
return status
}

function getAsciidocStatusWithDate (statusSections: string[]) {
let status: string[] = []
for (let i = 0; i < statusSections.length; i++) {
let currentStatusSection = statusSections[i]
if (/\d{1,4}-\d{1,2}-\d{1,2}/.test(currentStatusSection)) {
status.push(currentStatusSection)
}
}

return status
}

function setStatus (filePath, status) {
let fileData
let fileExt = Config.getDocExtension()
try {
fileData = fs.readFileSync(filePath, 'utf8')
} catch (error) {
console.log(error)
return []
}
let flag = false
let regExp = `## ${i18n.Status}`
let regExp
if (fileExt === 'adoc' || fileExt === 'asciidoc') {
regExp = `== ${i18n.Status}`
} else {
regExp = `## ${i18n.Status}`
}
let data: string[] = fileData.split('\n')
for (let i = 0; i < data.length; i++) {
let line: string = data[i]
if (flag && line[0] === '#') {
if (flag && (line[0] === '#' || line[0] === '=')) {
data.splice(i, 0, `${Utils.createDateString()} ${status}`)
data.splice(i + 1, 0, '')
return fs.writeFileSync(filePath, data.join('\n'))
Expand All @@ -63,15 +107,24 @@ function setStatus (filePath, status) {

function getAllStatus (filePath): string[] {
let fileData
let statusSections
let status
let fileExt = Config.getDocExtension()
try {
fileData = fs.readFileSync(filePath, 'utf8')
} catch (error) {
console.log(error)
return []
}
let tree = md.parse(fileData)
let statusSections = getStatusSection(tree)
let status = getStatusWithDate(statusSections)
if (fileExt === 'adoc' || fileExt === 'asciidoc') {
let tree = asciidoctor.load(fileData, { 'safe': 'safe', 'sourcemap': true })
statusSections = getAsciidocStatusSection(tree)
status = getAsciidocStatusWithDate(statusSections)
} else {
let tree = md.parse(fileData)
statusSections = getStatusSection(tree)
status = getStatusWithDate(statusSections)
}

if (status.length === 0) {
let lastStatusSection = statusSections[statusSections.length - 1]
Expand Down
6 changes: 4 additions & 2 deletions src/lib/base/GenerateBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { AbstractBuilder } from './AbstractBuilder'
import Utils from '../utils'
import getAdrFiles from '../helpers/getAdrFiles'
import * as walkSync from 'walk-sync'
import Config from '../Config'

export class GenerateBuilder implements AbstractBuilder {
path: string
Expand All @@ -23,12 +24,13 @@ export class GenerateBuilder implements AbstractBuilder {
let bodyString = this.bodyString
this.files.forEach(function (file) {
let fileName = file.relativePath
let fileExt = Config.getDocExtension()
let fileNameLength = fileName.length
let numberLength = Utils.getNumberLength(fileName) + '-'.length
let markdownWithPrefixLength = '.md'.length
let fileExtLength = ('.'+fileExt).length
let index = Utils.getIndexByString(fileName)
if (index) {
let decision = fileName.substring(numberLength, fileNameLength - markdownWithPrefixLength)
let decision = fileName.substring(numberLength, fileNameLength - fileExtLength)
handleBody(index, decision, file, bodyString, files.length)
}
})
Expand Down
3 changes: 3 additions & 0 deletions src/lib/base/ListGenerateBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ export class ListGenerateBuilder extends GenerateBuilder {
let fileData = fs.readFileSync(savePath + fileName, 'utf8')
let firstLine = fileData.split('\n')[0]
if (index) {
// for markdown support
let decision = firstLine.replace(/#\s\d+\.\s/g, '')
// for asciidoc support
decision = decision.replace(/=\s\d+\.\s/g, '')
handleBody(index, decision, file, bodyString, files.length)
}
})
Expand Down
Loading

0 comments on commit f92758c

Please sign in to comment.