Skip to content

Commit 8436232

Browse files
authored
Merge pull request #66 from ut-code/python-repl-id
ドキュメント内のpython-replにIDを追加
2 parents 64e688d + 7dc3f53 commit 8436232

File tree

9 files changed

+65
-61
lines changed

9 files changed

+65
-61
lines changed

app/[docs_id]/markdown.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ const components: Components = {
5151
),
5252
};
5353

54-
5554
export function Heading({
5655
level,
5756
children,
@@ -133,6 +132,11 @@ function CodeComponent({
133132
} else if (match[2] === "-repl") {
134133
// repl付きの言語指定
135134
// 現状はPythonのみ対応
135+
if (!match[3]) {
136+
console.warn(
137+
`${match[1]}-repl without terminal id! content: ${String(props.children).slice(0, 20)}...`
138+
);
139+
}
136140
switch (match[1]) {
137141
case "python":
138142
return (

public/docs/python-1.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ macOSでは、**Homebrew** というパッケージマネージャを使って
3636
このウェブサイトではドキュメント内に Python {process.env.PYODIDE_PYTHON_VERSION} の実行環境を埋め込んでいます。
3737
以下のように青枠で囲われたコード例には自由にPythonコードを書いて試すことができます。気軽に利用してください。
3838

39-
```python-repl
39+
```python-repl:1
4040
>>> message = "Hello, Python!"
4141
>>> print(message)
4242
Hello, Python!
@@ -60,19 +60,19 @@ Type "help", "copyright", "credits" or "license" for more information.
6060
### REPL の基本的な使い方
6161

6262
* **計算:** 数式を直接入力すると、計算結果が返ってきます。
63-
```python-repl
63+
```python-repl:2
6464
>>> 10 * 5 + 3
6565
53
6666
```
6767
* **変数と関数の利用:** 変数を定義したり、`print()`のような組み込み関数を呼び出したりできます。
68-
```python-repl
68+
```python-repl:3
6969
>>> greeting = "Hi there"
7070
>>> print(greeting)
7171
Hi there
7272
```
7373
* **ヘルプ機能:** `help()` と入力するとヘルプが表示されます。調べたいモジュールや関数名(例: `str`)を入力するとドキュメントが表示されます。
7474
* PCのターミナルで起動したREPLでは、対話的なヘルプモードが起動します。ヘルプモードを抜けるには `quit` と入力します。
75-
```python-repl
75+
```python-repl:4
7676
>>> help(str)
7777
Help on class str in module builtins:
7878

public/docs/python-2.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
Pythonの最も大きな特徴の一つは、変数の型を宣言する必要がないことです。変数への代入が、変数の作成と型の決定を同時に行います。
88

9-
```python-repl
9+
```python-repl:1
1010
>>> # 変数 message を作成し、文字列を代入
1111
>>> message = "Hello, Python!"
1212
>>> print(message)
@@ -19,7 +19,7 @@ Hello, Python!
1919

2020
さらに、Pythonは**動的型付け**言語です。これは、一度作成した変数に、異なる型のデータを再代入できることを意味します。`type()`関数を使うと、変数が現在どの型を参照しているかを確認できます。
2121

22-
```python-repl
22+
```python-repl:2
2323
>>> x = 10
2424
>>> type(x)
2525
<class 'int'>
@@ -39,7 +39,7 @@ Pythonには多くの組み込みデータ型がありますが、ここでは
3939

4040
Pythonは整数 (`int`) と浮動小数点数 (`float`) を区別します。
4141

42-
```python-repl
42+
```python-repl:3
4343
>>> # 整数 (int)
4444
>>> a = 10
4545
>>> type(a)
@@ -52,7 +52,7 @@ Pythonは整数 (`int`) と浮動小数点数 (`float`) を区別します。
5252

5353
四則演算は直感的に行えます。注意点として、除算 (`/`) は常に `float` を返します。整数除算を行いたい場合は (`//`) を使います。
5454

55-
```python-repl
55+
```python-repl:4
5656
>>> 10 / 3
5757
3.3333333333333335
5858
>>> 10 // 3
@@ -69,14 +69,14 @@ Pythonは整数 (`int`) と浮動小数点数 (`float`) を区別します。
6969

7070
文字列はシングルクォート (`'`) またはダブルクォート (`"`) で囲んで作成します。
7171

72-
```python-repl
72+
```python-repl:5
7373
>>> name = "Guido"
7474
>>> greeting = 'Hello'
7575
```
7676

7777
文字列の連結は `+` 演算子、繰り返しは `*` 演算子を使います。
7878

79-
```python-repl
79+
```python-repl:6
8080
>>> full_greeting = greeting + ", " + name + "!"
8181
>>> print(full_greeting)
8282
Hello, Guido!
@@ -86,7 +86,7 @@ Hello, Guido!
8686

8787
変数の値を文字列に埋め込む際には、**f-string (フォーマット済み文字列リテラル)** が非常に便利で推奨されています。文字列の前に `f` を付け、埋め込みたい変数を `{}` で囲みます。
8888

89-
```python-repl
89+
```python-repl:7
9090
>>> name = "Ada"
9191
>>> age = 36
9292
>>> message = f"My name is {name} and I am {age} years old."
@@ -98,7 +98,7 @@ My name is Ada and I am 36 years old.
9898

9999
真偽値は `True``False` の2つの値を持ちます(先頭が大文字であることに注意してください)。論理演算子には `and`, `or`, `not` を使います。
100100

101-
```python-repl
101+
```python-repl:8
102102
>>> is_active = True
103103
>>> has_permission = False
104104
>>> type(is_active)
@@ -120,15 +120,15 @@ False
120120

121121
`変数名: 型` のように記述します。
122122

123-
```python-repl
123+
```python-repl:9
124124
>>> # 型ヒントを付けた変数宣言
125125
>>> user_name: str = "Alice"
126126
>>> user_id: int = 123
127127
```
128128

129129
**重要な注意点:** 型ヒントはあくまで「ヒント」であり、**Pythonの実行エンジンはこれを強制しません**。つまり、型ヒントと異なる型の値を代入してもエラーにはなりません。
130130

131-
```python-repl
131+
```python-repl:10
132132
>>> user_id: int = 123
133133
>>> type(user_id)
134134
<class 'int'>

public/docs/python-3.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Pythonのプログラミングにおいて、データを効率的に扱う能
1212

1313
**基本的な使い方 (REPL実行例)**
1414

15-
```python-repl
15+
```python-repl:1
1616
>>> # リストの作成
1717
>>> fruits = ['apple', 'banana', 'cherry']
1818
>>> fruits
@@ -57,7 +57,7 @@ Pythonのプログラミングにおいて、データを効率的に扱う能
5757

5858
**基本的な使い方 (REPL実行例)**
5959

60-
```python-repl
60+
```python-repl:2
6161
>>> # タプルの作成 (丸括弧を使用)
6262
>>> coordinates = (10, 20)
6363
>>> coordinates
@@ -92,7 +92,7 @@ TypeError: 'tuple' object does not support item assignment
9292

9393
**基本的な使い方 (REPL実行例)**
9494

95-
```python-repl
95+
```python-repl:3
9696
>>> # 辞書の作成
9797
>>> person = {'name': 'Taro Yamada', 'age': 30, 'city': 'Tokyo'}
9898
>>> person
@@ -132,7 +132,7 @@ dict_items([('name', 'Taro Yamada'), ('age', 31), ('city', 'Tokyo'), ('job', 'En
132132

133133
**基本的な使い方 (REPL実行例)**
134134

135-
```python-repl
135+
```python-repl:4
136136
>>> # セットの作成 (重複した4は自動的に無視される)
137137
>>> numbers = {1, 2, 3, 4, 4, 5}
138138
>>> numbers
@@ -168,7 +168,7 @@ dict_items([('name', 'Taro Yamada'), ('age', 31), ('city', 'Tokyo'), ('job', 'En
168168

169169
**REPL実行例**
170170

171-
```python-repl
171+
```python-repl:5
172172
>>> numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
173173
174174
>>> # インデックス1から4の手前まで
@@ -200,7 +200,7 @@ dict_items([('name', 'Taro Yamada'), ('age', 31), ('city', 'Tokyo'), ('job', 'En
200200

201201
`for`ループで書く場合と、リスト内包表記で書く場合を比較してみましょう。
202202

203-
```python-repl
203+
```python-repl:6
204204
>>> # forループの場合
205205
>>> squares_loop = []
206206
>>> for i in range(10):
@@ -222,7 +222,7 @@ dict_items([('name', 'Taro Yamada'), ('age', 31), ('city', 'Tokyo'), ('job', 'En
222222

223223
**辞書内包表記**
224224

225-
```python-repl
225+
```python-repl:7
226226
>>> # 数値をキー、その2乗を値とする辞書を作成
227227
>>> square_dict = {x: x*x for x in range(5)}
228228
>>> square_dict
@@ -231,7 +231,7 @@ dict_items([('name', 'Taro Yamada'), ('age', 31), ('city', 'Tokyo'), ('job', 'En
231231

232232
**セット内包表記**
233233

234-
```python-repl
234+
```python-repl:8
235235
>>> # リスト内のユニークな数値の2乗のセットを作成
236236
>>> numbers = [1, 2, 2, 3, 4, 4, 5]
237237
>>> square_set = {x*x for x in numbers}

public/docs/python-4.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
Pythonの条件分岐は`if``elif`(else ifの略)、`else`を使って記述します。C言語やJavaのような波括弧`{}`は使わず、**コロン`:`とインデント(通常は半角スペース4つ)**でコードブロックを表現するのが最大の特徴です。
88

9-
```python-repl
9+
```python-repl:1
1010
>>> score = 85
1111
>>> if score >= 90:
1212
... print('優')
@@ -22,7 +22,7 @@ Pythonの条件分岐は`if`、`elif`(else ifの略)、`else`を使って記
2222

2323
条件式に`and``or``not`といった論理演算子も使用できます。
2424

25-
```python-repl
25+
```python-repl:2
2626
>>> temp = 25
2727
>>> is_sunny = True
2828
>>> if temp > 20 and is_sunny:
@@ -35,7 +35,7 @@ Pythonの条件分岐は`if`、`elif`(else ifの略)、`else`を使って記
3535

3636
Pythonの`for`ループは、他の言語の`for (int i = 0; i < 5; i++)`といったカウンタ変数を使うスタイルとは少し異なります。リストやタプル、文字列などの**イテラブル(反復可能)オブジェクト**から要素を1つずつ取り出して処理を実行します。これは、Javaの拡張for文やC\#`foreach`に似ています。
3737

38-
```python-repl
38+
```python-repl:3
3939
>>> fruits = ['apple', 'banana', 'cherry']
4040
>>> for fruit in fruits:
4141
... print(f"I like {fruit}")
@@ -49,7 +49,7 @@ I like cherry
4949

5050
決まった回数のループを実行したい場合は、`range()`関数が便利です。`range(n)`は0からn-1までの連続した数値を生成します。
5151

52-
```python-repl
52+
```python-repl:4
5353
>>> for i in range(5):
5454
... print(i)
5555
...
@@ -64,7 +64,7 @@ I like cherry
6464

6565
ループ処理の中で、要素のインデックス(番号)と値の両方を使いたい場合があります。そのような時は`enumerate()`関数を使うと、コードが非常にスッキリします。これは非常にPythonらしい書き方の一つです。
6666

67-
```python-repl
67+
```python-repl:5
6868
>>> fruits = ['apple', 'banana', 'cherry']
6969
>>> for i, fruit in enumerate(fruits):
7070
... print(f"Index: {i}, Value: {fruit}")
@@ -78,7 +78,7 @@ Index: 2, Value: cherry
7878

7979
`while`ループは、指定された条件が`True`である間、処理を繰り返します。ループを途中で抜けたい場合は`break`を、現在の回の処理をスキップして次の回に進みたい場合は`continue`を使用します。
8080

81-
```python-repl
81+
```python-repl:6
8282
>>> n = 0
8383
>>> while n < 5:
8484
... print(n)
@@ -95,7 +95,7 @@ Index: 2, Value: cherry
9595

9696
関数は`def`キーワードを使って定義します。ここでもコードブロックはコロン`:`とインデントで示します。値は`return`キーワードで返します。
9797

98-
```python-repl
98+
```python-repl:7
9999
>>> def greet(name):
100100
... """指定された名前で挨拶を返す関数""" # これはDocstringと呼ばれるドキュメント文字列です
101101
... return f"Hello, {name}!"
@@ -108,7 +108,7 @@ Hello, Alice!
108108
引数と返り値に**型アノテーション(型ヒント)**を付けることもできます。これはコードの可読性を高め、静的解析ツールによるバグの発見を助けますが、実行時の動作に直接影響を与えるものではありません。
109109
型アノテーションは `引数名: 型` のように記述し、返り値の型は `-> 型:` のように記述します。
110110

111-
```python-repl
111+
```python-repl:8
112112
>>> # typingモジュールからList型をインポート
113113
>>> from typing import List
114114
>>> def greet(name: str) -> str:
@@ -128,7 +128,7 @@ Pythonの関数は、非常に柔軟な引数の渡し方ができます。型
128128
* **キーワード引数 (Keyword Arguments):** `引数名=値`の形式で渡します。順序を問わないため、可読性が向上します。
129129
* **デフォルト引数値 (Default Argument Values):** 関数を定義する際に引数にデフォルト値を設定できます。呼び出し時にその引数が省略されると、デフォルト値が使われます。
130130

131-
```python-repl
131+
```python-repl:9
132132
>>> def describe_pet(animal_type: str, pet_name: str, owner_name: str = "Taro") -> None:
133133
... # この関数は何も値を返さないため、返り値の型は None となります
134134
... print(f"私には {animal_type} がいます。")
@@ -158,7 +158,7 @@ Pythonの関数は、非常に柔軟な引数の渡し方ができます。型
158158

159159
任意の数の**位置引数**をタプルとして受け取ります。型アノテーションでは `*args: 型` のように表現します。
160160

161-
```python-repl
161+
```python-repl:10
162162
>>> def sum_all(*numbers: int) -> int:
163163
... print(f"受け取ったタプル: {numbers}")
164164
... total = 0
@@ -178,7 +178,7 @@ Pythonの関数は、非常に柔軟な引数の渡し方ができます。型
178178

179179
任意の数の**キーワード引数**を辞書として受け取ります。型アノテーションでは `**kwargs: 型` のように表現します。どのような型の値も受け付ける場合は `Any` を使います。
180180

181-
```python-repl
181+
```python-repl:11
182182
>>> from typing import Any
183183
>>> def print_profile(**user_info: Any) -> None:
184184
... print(f"受け取った辞書: {user_info}")
@@ -198,7 +198,7 @@ city: Tokyo
198198

199199
構文: `lambda 引数: 式`
200200

201-
```python-repl
201+
```python-repl:12
202202
>>> # 通常の関数で2つの数を足す
203203
>>> def add(x: int, y: int) -> int:
204204
... return x + y

public/docs/python-5.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Pythonでは、**1つの `.py` ファイルが1つのモジュール**として
1010

1111
モジュールを利用するには `import` 文を使います。Pythonには多くの便利なモジュールが標準で用意されています(これらを**標準ライブラリ**と呼びます)。例えば、数学的な計算を行う `math` モジュールを使ってみましょう。
1212

13-
```python-repl
13+
```python-repl:1
1414
>>> # mathモジュールをインポート
1515
>>> import math
1616
>>> # mathモジュール内の変数や関数を利用する
@@ -31,7 +31,7 @@ Pythonでは、**1つの `.py` ファイルが1つのモジュール**として
3131

3232
* **`from ... import ...`**: モジュールから特定の関数や変数だけを取り込む
3333

34-
```python-repl
34+
```python-repl:2
3535
>>> from math import pi, sqrt
3636
>>>
3737
>>> print(pi) # 直接piを参照できる
@@ -42,7 +42,7 @@ Pythonでは、**1つの `.py` ファイルが1つのモジュール**として
4242
4343
* **`as` (別名)**: モジュールに別名をつけて利用する
4444
45-
```python-repl
45+
```python-repl:3
4646
>>> import math as m
4747
>>>
4848
>>> print(m.pi)
@@ -184,7 +184,7 @@ Pythonの大きな魅力の一つは、その「**バッテリー同梱 (Batteri
184184

185185
また、REPLの `help()``dir()` を使うと、モジュールの内容を簡単に確認できます。
186186

187-
```python-repl
187+
```python-repl:4
188188
>>> import datetime
189189
>>> # datetimeモジュールが持つ属性や関数のリストを表示
190190
>>> dir(datetime)
@@ -207,7 +207,7 @@ class date(builtins.object)
207207

208208
* **`os`**: オペレーティングシステムと対話するための機能を提供します。ファイルやディレクトリの操作、環境変数の取得などができます。
209209

210-
```python-repl
210+
```python-repl:5
211211
>>> import os
212212
>>> # カレントディレクトリのファイル一覧を取得
213213
>>> os.listdir('.')
@@ -219,7 +219,7 @@ class date(builtins.object)
219219
220220
* **`sys`**: Pythonインタプリタ自体を制御するための機能を提供します。コマンドライン引数の取得や、Pythonの検索パスの確認などができます。
221221
222-
```python-repl
222+
```python-repl:6
223223
>>> import sys
224224
>>> # Pythonのバージョンを表示
225225
>>> sys.version # 環境により異なります
@@ -228,7 +228,7 @@ class date(builtins.object)
228228
229229
* **`datetime`**: 日付や時刻を扱うための機能を提供します。
230230
231-
```python-repl
231+
```python-repl:7
232232
>>> import datetime
233233
>>> # 現在の日時を取得 (実行時刻による)
234234
>>> now = datetime.datetime.now()
@@ -241,7 +241,7 @@ class date(builtins.object)
241241
242242
* **`json`**: Web APIなどで広く使われているデータ形式であるJSONを扱うための機能を提供します。
243243
244-
```python-repl
244+
```python-repl:8
245245
>>> import json
246246
>>> # Pythonの辞書型データ
247247
>>> user = {"id": 1, "name": "Ken", "email": "[email protected]"}

0 commit comments

Comments
 (0)