Skip to content

Commit

Permalink
stephanrauh/ngx-extended-pdf-viewer#2554 improved the documentation o…
Browse files Browse the repository at this point in the history
…f the default options
  • Loading branch information
stephanrauh committed Sep 7, 2024
1 parent 96cc89b commit 87a726b
Show file tree
Hide file tree
Showing 8 changed files with 216 additions and 258 deletions.
88 changes: 7 additions & 81 deletions src/app/extended-pdf-viewer/attributes/attributes.component.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { HttpClient } from '@angular/common/http';
import { Component, ElementRef, AfterViewInit, OnInit } from '@angular/core';
import { Settings } from 'angular2-smart-table';
import { firstValueFrom } from 'rxjs';
import { isBrowser } from '../common/utilities';
import { compareFunction, convertMDToTable } from './md-to-table-converter';

@Component({
selector: 'app-attributes',
Expand All @@ -13,15 +13,6 @@ export class AttributesComponent implements OnInit, AfterViewInit {
public attributesAndEvents: Array<object> = [];
public lowLevelApi: Array<object> = [];

private compareFunction = (dir: number, a: string, b: string) => {
a = a.replace('[', '').replace(']', '').replace('(', '').replace(')', '').replace('<s>', '').replace('</s>', '');
b = b.replace('[', '').replace(']', '').replace('(', '').replace(')', '').replace('<s>', '').replace('</s>', '');
if (dir === 1) {
return a.localeCompare(b);
}
return b.localeCompare(a);
};

public attributeTableSettings: Settings = {
actions: {
edit: false,
Expand All @@ -36,7 +27,7 @@ export class AttributesComponent implements OnInit, AfterViewInit {
title: 'Attribute',
type: 'html',
sortDirection: 'asc',
compareFunction: this.compareFunction,
compareFunction,
},
description: {
title: 'Description',
Expand Down Expand Up @@ -67,7 +58,7 @@ export class AttributesComponent implements OnInit, AfterViewInit {
attribute: {
title: 'Attribute',
sortDirection: 'asc',
compareFunction: this.compareFunction,
compareFunction,
},
description: {
title: 'Description',
Expand All @@ -83,85 +74,20 @@ export class AttributesComponent implements OnInit, AfterViewInit {
},
};

constructor(private httpClient: HttpClient, private element: ElementRef) {}
constructor(private httpClient: HttpClient, private domElement: ElementRef) {}

public async ngOnInit(): Promise<void> {
this.attributesAndEvents = await this.convertMDToTable('/assets/extended-pdf-viewer/attributes/attributes.md');
this.lowLevelApi = await this.convertMDToTable('/assets/extended-pdf-viewer/attributes/low-level-api.md');
}

private async convertMDToTable(file: string): Promise<Array<any>> {
const source = await firstValueFrom(
this.httpClient.get(file, {
responseType: 'text',
})
);
const lines = this.splitLines(this.removeHeader(source));
return lines.map((line) => this.parseColumns(line));
this.attributesAndEvents = await convertMDToTable('/assets/extended-pdf-viewer/attributes/attributes.md', this.httpClient);
this.lowLevelApi = await convertMDToTable('/assets/extended-pdf-viewer/attributes/low-level-api.md', this.httpClient);
}

public ngAfterViewInit(): void {
if (isBrowser()) {
const html = this.element.nativeElement as HTMLElement;
const html = this.domElement.nativeElement as HTMLElement;
const inputFields = html.querySelectorAll('input');
inputFields.forEach((field) => {
field.placeholder = '(type to filter)';
});
}
}

private removeHeader(raw: string): string {
const parts = raw.split('------------------- |');
return parts[parts.length - 1];
}

private splitLines(raw: string): Array<string> {
return raw.trim().split('\n');
}

private parseColumns(line: string): object {
const columns = line.split('|');
return {
attribute: this.strikeThrough(columns[1].trim()),
defaultValue: columns[2].trim(),
description: this.findLinks(this.addCodeTags(columns[3].trim())),
};
}

private addCodeTags(text: string): string {
const fragments = text.split('`');
let result = fragments[0];
for (let i = 1; i < fragments.length; i++) {
if (i % 2 === 1) {
result += '<code>';
} else {
result += '</code>';
}
result += fragments[i];
}
return result;
}

private findLinks(text: string): string {
const s1 = text.indexOf('[');
const s2 = text.indexOf(']');
const b1 = text.indexOf('(');
const b2 = text.indexOf(')');
if (s1 >= 0 && s2 > s1 && b1 === s2 + 1 && b2 > b1) {
const link = text.substring(s1 + 1, s2);
const caption = text.substring(b1 + 1, b2);
text = text.substring(0, s1) + '<a target="#" href="' + link + '">' + caption + '</a>' + text.substring(b2 + 1);
return this.findLinks(text);
}
return text;
}

private strikeThrough(text: string): string {
if (text.startsWith('~~') && text.endsWith('~~')) {
text = text.replace('~~', '');
text = text.replace('~~', '');
return '<s>' + text + '</s>';
}
return text;
}
}
88 changes: 88 additions & 0 deletions src/app/extended-pdf-viewer/attributes/md-to-table-converter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import { HttpClient } from "@angular/common/http";
import { firstValueFrom } from "rxjs";

export async function convertMDToTable(file: string, httpClient: HttpClient): Promise<Array<any>> {
const source = await firstValueFrom(
httpClient.get(file, {
responseType: 'text',
})
);
const lines = splitLines(removeHeader(source));
return lines.map((line) => parseColumns(line));
}


function removeHeader(raw: string): string {
const parts = raw.split('------------------- |');
return parts[parts.length - 1];
}

function splitLines(raw: string): Array<string> {
return raw.trim().split('\n');
}

function parseColumns(line: string): object {
const columns = line.split('|');

let description = "";
let defaultValue = "";
if (columns.length === 5) {
description = findLinks(addCodeTags(columns[3].trim()));
defaultValue = addCodeTags(columns[2].trim());
} else {
description = findLinks(addCodeTags(columns[2].trim()));
defaultValue = "";
}

return {
attribute: strikeThrough(columns[1].trim()),
defaultValue,
description
};
}

function addCodeTags(text: string): string {
const fragments = text.split('`');
let result = fragments[0];
for (let i = 1; i < fragments.length; i++) {
if (i % 2 === 1) {
result += '<code>';
} else {
result += '</code>';
}
result += fragments[i];
}
return result;
}

function findLinks(text: string): string {
const s1 = text.indexOf('[');
const s2 = text.indexOf(']');
const b1 = text.indexOf('(');
const b2 = text.indexOf(')');
if (s1 >= 0 && s2 > s1 && b1 === s2 + 1 && b2 > b1) {
const link = text.substring(s1 + 1, s2);
const caption = text.substring(b1 + 1, b2);
text = text.substring(0, s1) + '<a target="#" href="' + link + '">' + caption + '</a>' + text.substring(b2 + 1);
return findLinks(text);
}
return text;
}

function strikeThrough(text: string): string {
if (text.startsWith('~~') && text.endsWith('~~')) {
text = text.replace('~~', '');
text = text.replace('~~', '');
return '<s>' + text + '</s>';
}
return text;
}

export const compareFunction = (dir: number, a: string, b: string) => {
a = a.replace('[', '').replace(']', '').replace('(', '').replace(')', '').replace('<s>', '').replace('</s>', '');
b = b.replace('[', '').replace(']', '').replace('(', '').replace(')', '').replace('<s>', '').replace('</s>', '');
if (dir === 1) {
return a.localeCompare(b);
}
return b.localeCompare(a);
};
Loading

0 comments on commit 87a726b

Please sign in to comment.