Skip to content

Commit

Permalink
feat(html): Parse newcomer report table
Browse files Browse the repository at this point in the history
  • Loading branch information
realth000 committed Sep 25, 2024
1 parent 247cdf3 commit 77a5257
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- 帖子:发帖和编辑时可设置阅读权限。
- 帖子:我的帖子页面中显示更多帖子信息。
- 帖子:我的帖子页面中修改帖子卡片布局。
- 帖子:支持解析新人报道分区的帖子中的新人报道表格。
- 消息:现在来源于帖子中的消息会直接跳转到帖子的相应楼层,而不经过消息详情页。
- app:支持设置和使用代理。
- 默认关闭,可在设置 -> 高级 -> 启用代理中打开。
Expand Down
5 changes: 5 additions & 0 deletions lib/i18n/strings.i18n.json
Original file line number Diff line number Diff line change
Expand Up @@ -616,5 +616,10 @@
"title": "Legacy data deleted",
"detail": "Seems this app have just upgraded from version older than 1.0, user login data and preferences are deleted because they are not usable any more.\nYou may need to login and set preferences once more. Sorry for inconvenience."
}
},
"html": {
"newcomerReportCard": {
"title": "Detail Info"
}
}
}
5 changes: 5 additions & 0 deletions lib/i18n/strings_zh-CN.i18n.json
Original file line number Diff line number Diff line change
Expand Up @@ -616,5 +616,10 @@
"title": "旧数据已清除",
"detail": "此应用似乎刚从低于1.0的版本升级上来,用户登录数据和设置偏好已不可使用,现已清除。\n您可能需要再登录一次并重新设置偏好,抱歉给您带来不便。"
}
},
"html": {
"newcomerReportCard": {
"title": "详细信息"
}
}
}
5 changes: 5 additions & 0 deletions lib/i18n/strings_zh-TW.i18n.json
Original file line number Diff line number Diff line change
Expand Up @@ -616,5 +616,10 @@
"title": "舊資料已清除",
"detail": "此應用程式似乎剛從低於1.0的版本升級上來,使用者登入資料和設定偏好已不可用,現已清除。\n您可能需要再登入一次並重新設定偏好,抱歉給您帶來不便。"
}
},
"html": {
"newcomerReportCard": {
"title": "詳細資料"
}
}
}
40 changes: 40 additions & 0 deletions lib/utils/html/html_muncher.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:tsdm_client/shared/models/models.dart';
import 'package:tsdm_client/utils/html/adaptive_color.dart';
import 'package:tsdm_client/utils/html/css_parser.dart';
import 'package:tsdm_client/utils/html/netease_card.dart';
import 'package:tsdm_client/utils/html/newcomer_report_card.dart';
import 'package:tsdm_client/utils/html/types.dart';
import 'package:tsdm_client/utils/logger.dart';
import 'package:tsdm_client/utils/show_bottom_sheet.dart';
Expand Down Expand Up @@ -317,6 +318,8 @@ final class _Muncher with LoggerMixin {
'pre' => _buildPre(node),
'details' => _buildDetails(node),
'iframe' => _buildIframe(node),
'table' when node.classes.contains('cgtl') =>
_buildNewcomerReport(node),
'ignore_js_op' ||
'table' ||
'tbody' ||
Expand Down Expand Up @@ -1009,6 +1012,43 @@ final class _Muncher with LoggerMixin {
return null;
}
List<InlineSpan>? _buildNewcomerReport(uh.Element element) {
// <table cellspacing="0" cellpadding="0" class="cgtl mbm">
// <caption>报到详细信息</caption>
// <tbody>
// <tr>
// <th valign="top">昵称:</th>
// <td> USER_NICKNAME</td>
// </tr>
//
// ...
//
// </tbody>
// </table>
final data = element
.querySelectorAll('tbody > tr')
.map(
(e) => (
e.querySelector('th')?.innerText.trim(),
e.querySelector('td')?.innerText.trim()
),
)
.whereType<(String, String)>()
.map(
(e) => NewcomerReportInfo(
title: e.$1,
data: e.$2,
),
)
.toList();
return [
WidgetSpan(
child: NewcomerReportCard(data),
),
];
}
/* Setup Functions */
/// Try parse color from [element].
Expand Down
107 changes: 107 additions & 0 deletions lib/utils/html/newcomer_report_card.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import 'package:collection/collection.dart';
import 'package:flutter/material.dart';
import 'package:tsdm_client/constants/layout.dart';
import 'package:tsdm_client/i18n/strings.g.dart';

/// Table info in newcomer report table.
///
/// Each info occupies a row in table.
class NewcomerReportInfo {
/// Constructor.
NewcomerReportInfo({
required this.title,
required this.data,
});

/// Info title.
final String title;

/// Info data, may be empty string.
final String data;
}

/// Card for newcomer report table in thread.
///
/// Only used in newcomer subreddit.
class NewcomerReportCard extends StatelessWidget {
/// Constructor.
const NewcomerReportCard(this.data, {super.key});

/// Data in table.
final List<NewcomerReportInfo> data;

@override
Widget build(BuildContext context) {
return Card(
margin: EdgeInsets.zero,
child: Padding(
padding: edgeInsetsL12T12R12B12,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
mainAxisSize: MainAxisSize.min,
children: [
Icon(
Icons.article_outlined,
color: Theme.of(context).colorScheme.primary,
),
sizedBoxW4H4,
Text(
context.t.html.newcomerReportCard.title,
style: Theme.of(context).textTheme.titleLarge?.copyWith(
color: Theme.of(context).colorScheme.primary,
),
),
],
),
sizedBoxW8H8,
SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: Table(
defaultColumnWidth: const IntrinsicColumnWidth(),
columnWidths: const <int, TableColumnWidth>{
0: FixedColumnWidth(100),
},
defaultVerticalAlignment: TableCellVerticalAlignment.middle,
children: data
.mapIndexed(
(idx, e) => TableRow(
decoration: BoxDecoration(
color: idx.isOdd
? Theme.of(context)
.colorScheme
.surfaceContainerHigh
: null,
),
children: [
TableCell(
child: Text(
e.title,
style: Theme.of(context)
.textTheme
.bodyMedium
?.copyWith(
color:
Theme.of(context).colorScheme.secondary,
),
),
),
TableCell(
child: Text(
e.data,
style: Theme.of(context).textTheme.bodyMedium,
),
),
],
),
)
.toList(),
),
),
],
),
),
);
}
}

0 comments on commit 77a5257

Please sign in to comment.