Skip to content

Commit

Permalink
feat(parser): control 상속관계로 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
hahnlee committed May 18, 2024
1 parent bfcd4c0 commit 4573422
Show file tree
Hide file tree
Showing 10 changed files with 103 additions and 58 deletions.
50 changes: 36 additions & 14 deletions packages/parser/src/models/controls/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,38 +15,60 @@
*/

import { CommonCtrlID, OtherCtrlID } from '../../constants/ctrl-id.js'
import { SectionTagID } from '../../constants/tag-id.js'
import type { ParseOptions } from '../../types/parser.js'
import { ByteReader } from '../../utils/byte-reader.js'
import type { PeekableIterator } from '../../utils/generator.js'
import type { HWPRecord } from '../record.js'
import { HWPVersion } from '../version.js'
import { SectionControl } from './section.js'
import { PictureControl } from './shapes/picture.js'
import { GenShapeObjectControl } from './shapes/shape.js'
import { TableControl } from './table.js'
import { UnknownControl } from './unknown.js'

export type ControlContent =
| TableControl
| SectionControl
| GenShapeObjectControl
| PictureControl
| UnknownControl

export function parseControl(
function mapControl(
ctrlId: number,
current: HWPRecord,
iterator: PeekableIterator<HWPRecord>,
version: HWPVersion,
options: ParseOptions,
): ControlContent {
) {
switch (ctrlId) {
case OtherCtrlID.Section:
return SectionControl.fromRecord(current, iterator, version)
return SectionControl.fromRecord(ctrlId, current, iterator, version)
case CommonCtrlID.Table:
return TableControl.fromRecord(current, iterator, version, options)
return TableControl.fromRecord(
ctrlId,
current,
iterator,
version,
options,
)
case CommonCtrlID.GenShapeObject:
return GenShapeObjectControl.fromRecord(current, iterator, version, options)
return GenShapeObjectControl.fromRecord(
ctrlId,
current,
iterator,
version,
options,
)
default:
return UnknownControl.fromRecord(current, iterator)
return UnknownControl.fromRecord(ctrlId, current, iterator)
}
}

export function parseControl(
iterator: PeekableIterator<HWPRecord>,
version: HWPVersion,
options: ParseOptions,
) {
const current = iterator.next()
if (current.id !== SectionTagID.HWPTAG_CTRL_HEADER) {
throw new Error('Control: Record has wrong ID')
}

const reader = new ByteReader(current.data)
const id = reader.readUInt32()

return mapControl(id, current, iterator, version, options)
}
19 changes: 19 additions & 0 deletions packages/parser/src/models/controls/control.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Copyright Han Lee <[email protected]> and other contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

export class Control {
constructor(public id: number) {}
}
31 changes: 2 additions & 29 deletions packages/parser/src/models/controls/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,5 @@
* limitations under the License.
*/

import { ByteReader } from '../../utils/byte-reader.js'
import type { PeekableIterator } from '../../utils/generator.js'
import type { HWPRecord } from '../record.js'
import { SectionTagID } from '../../constants/tag-id.js'
import { type ControlContent, parseControl } from './content.js'
import { HWPVersion } from '../version.js'
import type { ParseOptions } from '../../types/parser.js'

export class Control {
constructor(public id: number, public content: ControlContent) {}

static fromRecords(
iterator: PeekableIterator<HWPRecord>,
version: HWPVersion,
options: ParseOptions,
) {
const current = iterator.next()
if (current.id !== SectionTagID.HWPTAG_CTRL_HEADER) {
throw new Error('Control: Record has wrong ID')
}

const reader = new ByteReader(current.data)
const id = reader.readUInt32()

const content = parseControl(id, current, iterator, version, options)

return new Control(id, content)
}
}
export * from './control.js'
export * from './section.js'
10 changes: 8 additions & 2 deletions packages/parser/src/models/controls/section.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ import type { PeekableIterator } from '../../utils/generator.js'
import { Border } from '../doc-info/border-fill.js'
import type { HWPRecord } from '../record.js'
import { HWPVersion } from '../version.js'
import { Control } from './control.js'
import { PageDefinition } from './page-definition.js'

export class SectionControl {
export class SectionControl extends Control {
constructor(
public id: number,
/** 컨트롤 ID */
public ctrlId: number,
/** 머리말을 감출지 여부 */
Expand Down Expand Up @@ -81,9 +83,12 @@ export class SectionControl {
/** 미주 모양 정보 */
public endnoteShape: FootnoteEndnoteShape,
public unknown: ArrayBuffer,
) {}
) {
super(id)
}

static fromRecord(
id: number,
record: HWPRecord,
iterator: PeekableIterator<HWPRecord>,
version: HWPVersion,
Expand Down Expand Up @@ -146,6 +151,7 @@ export class SectionControl {
// TODO: (@hahnlee) 바탕쪽 정보 관련된 파싱 추가하기

return new SectionControl(
id,
ctrlId,
hideHeader,
hideFooter,
Expand Down
2 changes: 1 addition & 1 deletion packages/parser/src/models/controls/shapes/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ export function parseContent(
case CommonCtrlID.Picture:
return PictureRecord.fromRecords(iterator)
default:
return UnknownControl.fromRecord(current, iterator)
return UnknownControl.fromRecord(elementProperties.ctrlId, current, iterator)
}
}
10 changes: 8 additions & 2 deletions packages/parser/src/models/controls/shapes/shape.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,17 @@ import type { PeekableIterator } from '../../../utils/generator.js'
import type { HWPRecord } from '../../record.js'
import type { HWPVersion } from '../../version.js'
import { CommonProperties } from '../common-properties.js'
import { Control } from '../control.js'
import { DrawText } from '../draw-text.js'
import { ElementProperties } from '../element-properties.js'
import { type ShapeObjectContent, parseContent } from './content.js'

/**
* 그리기 객체
*/
export class GenShapeObjectControl {
export class GenShapeObjectControl extends Control {
constructor(
public id: number,
/** 개체 공통 속성 */
public commonProperties: CommonProperties,
/** 개체 요소 속성 */
Expand All @@ -37,9 +39,12 @@ export class GenShapeObjectControl {
public drawText: DrawText | null,
/** 컨텐츠 */
public content: ShapeObjectContent,
) {}
) {
super(id)
}

static fromRecord(
id: number,
record: HWPRecord,
iterator: PeekableIterator<HWPRecord>,
version: HWPVersion,
Expand All @@ -62,6 +67,7 @@ export class GenShapeObjectControl {
const content = parseContent(elementProperties, record, iterator)

return new GenShapeObjectControl(
id,
commonProperties,
elementProperties,
drawText,
Expand Down
18 changes: 14 additions & 4 deletions packages/parser/src/models/controls/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,21 @@ import { CommonProperties } from './common-properties.js'
import { SectionTagID } from '../../constants/tag-id.js'
import { ParagraphList } from './paragraph-list.js'
import type { ParseOptions } from '../../types/parser.js'
import { Control } from './control.js'

export class TableControl {
export class TableControl extends Control {
constructor(
public id: number,
/** 개체 공통 속성 */
public commonProperties: CommonProperties,
public record: TableRecord,
public cells: Cell[],
) {}
) {
super(id)
}

static fromRecord(
id: number,
record: HWPRecord,
iterator: PeekableIterator<HWPRecord>,
version: HWPVersion,
Expand Down Expand Up @@ -60,7 +65,7 @@ export class TableControl {
cells.push(Cell.fromRecords(iterator, version, options))
}

return new TableControl(commonProperties, tableRecord, cells)
return new TableControl(id, commonProperties, tableRecord, cells)
}
}

Expand Down Expand Up @@ -220,7 +225,12 @@ export class Cell {
}

const reader = new ByteReader(record.data)
const paragraphs = ParagraphList.fromReader(reader, iterator, version, options)
const paragraphs = ParagraphList.fromReader(
reader,
iterator,
version,
options,
)

const column = reader.readUInt16()
const row = reader.readUInt16()
Expand Down
15 changes: 11 additions & 4 deletions packages/parser/src/models/controls/unknown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,18 @@
import type { PeekableIterator } from '../../utils/generator.js'
import { collectChildren } from '../../utils/record.js'
import type { HWPRecord } from '../record.js'
import { Control } from './control.js'

export class UnknownControl {
constructor(public records: HWPRecord[]) {}
export class UnknownControl extends Control {
constructor(public id: number, public records: HWPRecord[]) {
super(id)
}

static fromRecord(current: HWPRecord, iterator: PeekableIterator<HWPRecord>) {
return new UnknownControl(collectChildren(iterator, current.level))
static fromRecord(
id: number,
current: HWPRecord,
iterator: PeekableIterator<HWPRecord>,
) {
return new UnknownControl(id, collectChildren(iterator, current.level))
}
}
3 changes: 2 additions & 1 deletion packages/parser/src/models/paragraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { collectChildren } from '../utils/record.js'
import { RangeTag } from './range-tag.js'
import { CharShape } from './char-shape.js'
import type { ParseOptions } from '../types/parser.js'
import { parseControl } from './controls/content.js'

export class Paragraph {
constructor(
Expand Down Expand Up @@ -100,7 +101,7 @@ export class Paragraph {

const controls: Control[] = chars
.extendedControls()
.map(() => Control.fromRecords(iterator, version, options))
.map(() => parseControl(iterator, version, options))

const unknown = collectChildren(iterator, current.level)

Expand Down
3 changes: 2 additions & 1 deletion packages/parser/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"compilerOptions": {
"module": "NodeNext",
"lib": ["esnext"],
"strict": true
"strict": true,
"skipLibCheck": true
}
}

0 comments on commit 4573422

Please sign in to comment.